简体   繁体   English

R中的仿真

[英]Simulation in R

I need to simulate 100 datasets of n=100. 我需要模拟100个n = 100的数据集。 I am trying something like: 我正在尝试类似的东西:

x1 = NULL 
for (i in 1:100){
set.seed(i)
x1[i] <- sample(1:4, 100, replace=TRUE)
}

But I keep getting errors saying: In x1[i] <- sample(1:4, 100, replace = TRUE) : number of items to replace is not a multiple of replacement length 但是我不断收到错误消息:x1 [i] <-sample(1:4,100,replace = TRUE):要替换的项目数不是替换长度的倍数

I am stumped. 我感到难过。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

You can also use the "replicate" function like this: 您也可以像这样使用“复制”功能:

x1 <- replicate(100, sample(1:4,100,replace=T))
split(x1, col(x1))

If you are interested, there's an excellent ebook called "R Fundamentals & Graphics" for beginners in R. 如果您有兴趣,可以为R的初学者阅读一本优秀的电子书,称为“ R基础和图形”。

All you need to do is change x1[i] to x1[[i]] 您需要做的就是将x1[i]更改为x1[[i]]

Paul 保罗

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM