从数据帧中采样行并保存未选择的行

时间:2017-12-08 23:43:53

标签: r

我正在试图从数据框中随机选择行;但是,我也需要未经选择的。我按照以下步骤进行了

#grades is data with size 100
half-grades = grades[sample(nrow(grades), 50), ]

如何保存未选择的?

3 个答案:

答案 0 :(得分:0)

假设您正在寻找甚至50%的分割,代码如下:

library(caTools)
split = sample.split(grades,SplitRatio=0.5)
half-grades1 = subset(grades, split == TRUE)
half-grades2 = subset(grades, split == FALSE)

答案 1 :(得分:0)

使用caret::createDataPartition()可轻松完成此操作。

library(caret)
theIndex <- createDataPartition(grades, p = .50,list=FALSE)
sampled <- grades[theIndex,]
notSampled <- grades[-theIndex,]

答案 2 :(得分:0)

set.seed(1234)
half-grades=grades[-(sample(nrow(grades), 50)), ]