重复互补子集

时间:2014-08-07 06:24:17

标签: r

我想重复将一个集合划分为两个具有已知大小的互补子集,并将它们保留为两个矩阵的列。例如,假设主集是{1,2,...,10},第一个样本的大小是8,我想重复采样3次。我希望:

     [,1] [,2] [,3]
[1,]   10    9    1
[2,]    8    1   10
[3,]    3    7    5
[4,]    4    2    3
[5,]    1    8    8
[6,]    6    4    2
[7,]    9    5    7
[8,]    5   10    6

     [,1] [,2] [,3]
[1,]    2    3    4
[2,]    7    6    9

知道如何在R中实现它,避免for循环?

1 个答案:

答案 0 :(得分:2)

我会使用replicate + sample,如下所示:

set.seed(1)                             # Just so you can replicate my results
A <- replicate(3, sample(10, 8, FALSE)) # Change 3 to the number of replications
A
#      [,1] [,2] [,3]
# [1,]    3    7    8
# [2,]    4    1    9
# [3,]    5    2    4
# [4,]    7    8    6
# [5,]    2    5    7
# [6,]    8   10    2
# [7,]    9    4    3
# [8,]    6    6    1

对于另一组,我会使用apply + setdiff,如下所示:

B <- apply(A, 2, function(x) setdiff(1:10, x))
B
#      [,1] [,2] [,3]
# [1,]    1    3    5
# [2,]   10    9   10

@thelatemail建议的另一个选项(效率更高)就是创建使用replicate来创建原始矩阵,并使用基本子集来创建单独的矩阵。

A <- replicate(3, sample(10))
B <- A[-(seq_len(8)), ]
A <- A[seq_len(8), ]