Generate sample with set.seed() function in R

What is set.seed() function in R and why to use it ? : set.seed() function in R is used to reproduce results i.e.  it produces the same sample again and again. When we generate randoms numbers without set.seed() function it will produce different samples at different time of execution. let see how to generate stable sample of random numbers with set.seed() function in R with example. set.seed() of a dataframe is also depicted as an example.

  • set.seed() function generate sample of a vector
  • Generate random sample of a dataframe using set.seed() in R

 

Syntax for set.seed() function in R:

set.seed(n)

where n is a seed number which is an integer value.

The seed number (n) you choose is the starting point used in the generation of a sequence of random numbers. Which is why you’ll obtain the same results given the same seed number.

 

Example of set.seed function in R:

generate numeric samples without set.seed() will result in multiple outputs when we run multiple times

rnorm(5)

rnorm(5)

when we run above rnorm function, it will generate different set of five numbers at each time

output:

[1]  1.7150650  0.4609162 -1.2650612 -0.6868529 -0.4456620

[1]  1.2240818  0.3598138  0.4007715  0.1106827 -0.5558411

 

generate numeric samples with set.seed() will result in same outputs when we run multiple times

# set.seed function in R

set.seed(123)
rnorm(5)

set.seed(123)
rnorm(5)

when we run above rnorm function, it will generate same set of five numbers at each time because of set.seed() function

output:

[1] -0.56047565 -0.23017749  1.55870831  0.07050839  0.12928774 

[1] -0.56047565 -0.23017749  1.55870831  0.07050839  0.12928774

 

 

Example 2 of set.seed function:

generate character samples without set.seed() will result in multiple outputs when we run multiple times

sample(LETTERS, 6)

sample(LETTERS, 6)

when we run above sample function, it will generate different set of 6 letters at each time

output:

[1] “Y” “L” “Q” “N” “C” “S”

[1] “G” “B” “H” “V” “T” “O”

 

generate character samples with set.seed() will result in single outputs when we run multiple times

# set.seed function in R

set.seed(321)
sample(LETTERS, 6)

set.seed(321)
sample(LETTERS, 6)

when we run above sample function, it will generate same set of 6 letters at each time because of set.seed() function

output:

[1] “Y” “X” “F” “Z” “I” “H” 

[1] “Y” “X” “F” “Z” “I” “H”

 

Generate Random sample in R dataframe using set.seed() function:

Let’s see below example on set.seed() function of a dataframe to extract random sample of the dataframe.

 
# set.seed function in R dataframe

set.seed(123)
index <- sample(1:nrow(iris), 10)
index

iris[index, ]

the random row numbers from sample() function is created and passed to iris dataset to get the random samples from iris data set using set.seed() function

Generate sample with set.seed() function in R 1

 

previous small set.seed function in r                                                                                                           next small set.seed function in r