从具有动态列索引的二进制矩阵中选择子集

时间:2013-01-13 04:37:23

标签: r matrix

一些问题,R语言可能有优雅的解决方案......

给定,矩阵m包含二进制值1和0,以及列索引的向量v

  1. 我如何编写一个函数来提取m中的所有行 由v?
  2. 中的整数索引的每列中的值1
  3. 作为一个额外的功能,如何返回行索引 相应的行?
  4. 如果我用一个例子来说明,那可能是最好的......

    假设我要求的逻辑驻留在函数selectByIndi​​ces(matrix,indexVector)中。

    所以如果我们有矩阵(或者可能是等效的数据帧):

     >(m= matrix(c( 1, 0, 1, 1, 1,0, 1, 1, 0, 1,1, 0, 1, 1, 0,1, 1, 1, 
       0, 1,0, 1, 0, 0, 1), 5))
    
             [,1] [,2] [,3] [,4] [,5]
      [1,]    1    0    1    1    0
      [2,]    0    1    0    1    1
      [3,]    1    1    1    1    0
      [4,]    1    0    1    0    0
      [5,]    1    1    0    1    1
    

    和索引向量:

     >c1 = c(1,3,4)
     >c2 =  c(4,5)
     >c3 =  c(1,3,5)
    

    该函数的行为类似于:

     >selectByIndices( m, c1)
    
            [,1] [,2] [,3] [,4] [,5]
      [1,]    1    0    1    1    0
      [3,]    1    1    1    1    0
    
    
     >selectByIndices( m, c2)
    
            [,1] [,2] [,3] [,4] [,5]
      [2,]    0    1    0    1    1
      [5,]    1    1    0    1    1
    
    
     >selectByIndices( m, c3)
    
        #no rows (i.e. empty collection) returned
    

    希望它足够清楚,先谢谢你的帮助。

2 个答案:

答案 0 :(得分:2)

## Create a function that extracts the qualifying rows
f <- function(m, j) {
    m[rowSums(m[, j]) == length(j),]
    # m[apply(m[, j], 1, function(X) all(X==1)),] ## This would also work
    # which(rowSums(m[, j]) == length(j))         ## & this would get row indices
}

## Try it out
f(m, c1)
#      [,1] [,2] [,3] [,4] [,5]
# [1,]    1    0    1    1    0
# [2,]    1    1    1    1    0

f(m, c2)
#      [,1] [,2] [,3] [,4] [,5]
# [1,]    0    1    0    1    1
# [2,]    1    1    0    1    1

答案 1 :(得分:0)

> selectRows <- function(mat, rown) suppressWarnings(mat[apply( mat[, rown], 1, all) , ])
> selectRows(m, c1)
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    0    1    1    0
[2,]    1    1    1    1    0

>  whichRows <-function(mat, rown) suppressWarnings( which( apply( mat[, rown], 1, all) ) )
> whichRows(m, c1)
[1] 1 3