Generate Data sets of same Random Values in R Programming – set.seed() Function

set.seed() function in R Language is used to create random numbers which can be reproduced. It helps in creating same random numbers each time a random function is called. This helps in creating repeatable data sets for a analysis.

Syntax: set.seed(n)

Parameters:
n: seeds for repeatable data sets

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to create repeatable data sets
  
# Setting seeds
set.seed(10)
  
# Creating data set of random values
x <- rnorm(15)
x
  
# Setting seed again for another data set
set.seed(10)
  
# Creating another data set
y <- rnorm(15)
y
  
# Checking if both are equal
identical(x, y)

chevron_right


Output:

 [1]  0.01874617 -0.18425254 -1.37133055 -0.59916772  0.29454513  0.38979430
 [7] -1.20807618 -0.36367602 -1.62667268 -0.25647839  1.10177950  0.75578151
[13] -0.23823356  0.98744470  0.74139013
 [1]  0.01874617 -0.18425254 -1.37133055 -0.59916772  0.29454513  0.38979430
 [7] -1.20807618 -0.36367602 -1.62667268 -0.25647839  1.10177950  0.75578151
[13] -0.23823356  0.98744470  0.74139013
[1] TRUE

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to create repeatable data sets
  
# Setting seeds
set.seed(10)
  
# Creating data set of random values
x <- rexp(15)
x
  
# Setting seed again for another data set
set.seed(10)
  
# Creating another data set
y <- rexp(15)
y
  
# Checking if both are equal
identical(x, y)

chevron_right


Output:

 [1] 0.01495641 0.92022120 0.75215894 1.57504185 0.23165862 1.08667300
 [7] 2.32762287 0.72912382 1.28831010 0.67226829 0.42652979 1.11542195
[13] 1.31654707 0.41329383 0.67657533
 [1] 0.01495641 0.92022120 0.75215894 1.57504185 0.23165862 1.08667300
 [7] 2.32762287 0.72912382 1.28831010 0.67226829 0.42652979 1.11542195
[13] 1.31654707 0.41329383 0.67657533
[1] TRUE



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.