对行号进行抽样,保存抽出样本的行号,在这些行号前加减号,取数据集的子集即可
> set.seed(1234)
> ( x <- data.frame(a=sample(c(1,0),10,replace=T),b=11:20) )
a b
1 1 11
2 0 12
3 0 13
4 0 14
5 0 15
6 0 16
7 1 17
8 1 18
9 0 19
10 0 20
> ( row_1 <- which(x$a==1) )
[1] 1 7 8
> ( row_0 <- which(x$a==0) )
[1] 2 3 4 5 6 9 10
> ( index1 <- sample(row_1,2) )
[1] 8 7
> ( index0 <- sample(row_0,2) )
[1] 3 9
#抽样
> ( sub1 <- x[c(index1,index0),] )
a b
8 1 18
7 1 17
3 0 13
9 0 19
#剩余
> ( sub2 <- x[-c(index1,index0),] )
a b
1 1 11
2 0 12
4 0 14
5 0 15
6 0 16
10 0 20