在 R 编程中生成相同随机值的数据集 – set.seed()函数
R 语言中的set.seed()
函数用于创建可以复制的随机数。每次调用随机函数时,它都有助于创建相同的随机数。这有助于为分析创建可重复的数据集。
Syntax: set.seed(n)
Parameters:
n: seeds for repeatable data sets
示例 1:
# 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)
输出:
[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
示例 2:
# 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)
输出:
[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