Open In App

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

In this article, we will discuss how we Generate Data sets of the same Random Values in the R Programming Language using set.seed() Function.

What is to set seed in R?

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



Syntax: set.seed(n)

Parameters:



n: seeds for repeatable data sets

Create Random Numbers without a set.seed() function in R




rnorm(10)

Output:

 [1]  0.38984324 -0.62124058 -2.21469989  1.12493092 -0.04493361
 [6] -0.01619026  0.94383621  0.82122120  0.59390132  0.91897737

Why set.seed() is required

In R, the set.seed() function is not mandatory for all analyses. However, it is recommended to use it in some instances. There are various reasons why using set.seed() becomes advantageous.

R program to create repeatable data sets




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

Output:

[1] "The random values of x"

 [1]  0.01874617 -0.18425254 -1.37133055 -0.59916772  0.29454513
 [6]  0.38979430 -1.20807618 -0.36367602 -1.62667268 -0.25647839
[11]  1.10177950  0.75578151 -0.23823356  0.98744470  0.74139013

[1] "The random values of y"

 [1]  0.01874617 -0.18425254 -1.37133055 -0.59916772  0.29454513
 [6]  0.38979430 -1.20807618 -0.36367602 -1.62667268 -0.25647839
[11]  1.10177950  0.75578151 -0.23823356  0.98744470  0.74139013

[1] "The identical values of x and y"

[1] TRUE

R program to create repeatable data sets




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

Output:

 [1] "The random values of x"

[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] "The random values of y"

[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] "The equal values of x and y"

[1] TRUE

Article Tags :