使用R在矩阵的每一行中进行条件采样

时间:2013-06-20 14:16:13

标签: r data-manipulation

我试图从类似于以下代码创建的矩阵中抽样:

    set.seed(17)
    x = sample(0:1, 100, replace=T)
    x = matrix(x, 20)
    xrowsum = apply(x, 1, sum)
    x = x[xrowsum > 2, ]
    x
    #      [,1] [,2] [,3] [,4] [,5]
    # [1,]    0    1    1    0    1
    # [2,]    1    1    1    1    0
    # [3,]    1    1    1    0    1
    # [4,]    0    1    1    0    1
    # [5,]    0    1    1    0    1
    # [6,]    1    1    0    0    1
    # [7,]    1    1    1    0    1
    # [8,]    1    1    1    0    0
    # [9,]    1    0    1    1    1
    #[10,]    1    1    1    1    1
    #[11,]    1    1    0    1    1

目标是从每一行中抽取两个项目,但只有那些条目等于1,并将0分配给该行的其余部分。一个预期结果可能如下所示。

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

1 个答案:

答案 0 :(得分:1)

使用apply的一种解决方案。

t(apply(x, 1, function(row) {
   out <- rep(0, length(row))
   one.idx <- which(row == 1)
   stopifnot(length(one.idx) >= 2)
   keep.idx <- sample(one.idx, 2)
   out[keep.idx] <- 1
   out
}))

如果行少于两个1,则会出错。请澄清这不是您希望如何处理该特定情况。