使用数据帧的模拟数据

时间:2015-04-05 20:24:05

标签: r

我想模拟现有数据集以获得最佳模型。考虑以下数据集,我想在R中使用此数据创建100或1000个样本。任何帮助?

x<-data.frame( A=c("A","B","M","N","P"),
B=c("G","N","P","R","I"),
C=c("T","F","L","U","U"),
D=c(9,12,13,13,12),
E=c(14,17,10,19,10),
F=c(10,12,19,17,15), 
G=c(7,12,10,14,15), 
H=c(18,19,19,12,12),
I=c("K","L","M","F","D"),
J=c("C","V","O","N","F"),
K=c("G","N","P","P","I"))

1 个答案:

答案 0 :(得分:1)

如果您想要1000个数据集,请尝试将它们全部放入列表中。

lapply(1:1000, function(i, x) x[sample(nrow(x), size=1000, replace=TRUE), ], x)

虽然我不确定从较少观察的样本中使用大小为1000的模拟数据集的价值是多少。我相信Ben Bolker对size=nrow(x)的建议可能是更合理的方法。