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:
# R program to create repeatable data sets # Setting seedsset.seed(10) # Creating data set of random valuesx <- rnorm(15)x # Setting seed again for another data setset.seed(10) # Creating another data sety <- rnorm(15)y # Checking if both are equalidentical(x, y) |
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:
# R program to create repeatable data sets # Setting seedsset.seed(10) # Creating data set of random valuesx <- rexp(15)x # Setting seed again for another data setset.seed(10) # Creating another data sety <- rexp(15)y # Checking if both are equalidentical(x, y) |
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



