从R中的数据帧中随机抽取行。如何使用未采样的行?

时间:2017-03-14 02:08:31

标签: r dataframe random

从数据框中随机取样而无需替换。我将该样本分配给新的数据框。我想使用该样本中遗留的行。

为了创建样本,我使用了:

telemarketing_sample <- telemarketing[sample(1:nrow(telemarketing), 30000, replace=FALSE),]

1 个答案:

答案 0 :(得分:4)

通常,提供一些您已尝试过给人们开始的代码是很有帮助的。与此同时,这样的事情应该有效:

#generate some toy data
x <- data.frame(id = 1:100, y = rep(c(0,1),50))

#get a sample of 10
samp <- sample(nrow(x),10)

#data in the sample
in <- x[samp,]

#data not in the sample
out <- x[-samp,]