qed
qed

Reputation: 23094

Conditional sampling in each row of a matrix using R

I am trying to sample from something similar to the matrix created by the following code:

    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

The goal is to sample two items from each row, but only those entries equal to 1, and assign 0 to the rest of the row. One expected result might look like this.

    #      [,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

Upvotes: 1

Views: 680

Answers (1)

flodel
flodel

Reputation: 89057

One solution using 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
}))

It will error out if a row has less than two 1. Please clarify if this is not how you want to handle that particular case.

Upvotes: 1

Related Questions