Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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

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.

  • Reproducibility: Seed setting guarantees that the sequence of random numbers created by our code will remain constant across different runs. This is necessary when we want other people to reproduce our results or even if we need to reproduce our own findings at a later date. Without a set seed, the randomness introduced by R’s random number generator could lead to different results each time the code is executed.
  • Comparisons and Testing: To ensure that any observed differences are not due to random chance, a fixed seed should be present when comparing algorithms, models or methods among others.
  • Debugging: While debugging our code, having a fixed seed can simplify the process. Absence of a seed may lead to inconsistent results which may complicate the process of identifying errors or unexpected behaviors in our code.
  • Communication and Documentation: our code becomes more transparent once we employ set.seed(). By specifying the seed when sharing the code with others they can always get exactly the same results as ours.

R program to create repeatable data sets

R




# 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




# 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


Last Updated : 28 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads