开发者

Filtering the table based on rowname

  1. I do have a list of elements in ab,

    ab <- list("M","O","E","P","Q","S","T","N","V","R")
    
  2. In other list tb, is a list of 2*2 tables like,

    Sample data set,

    n <- c("M", "N", "O")
    tb <- lapply(1:10, function(i)matrix(sample(4), 2, 2, 
                                         dimnames=list(n[sample(3,2)], 
                                                       n[sample(3,2)])))
    names(tb) <- paste(1:10)
    
  3. We only need to filter those tables in which the rowname of 1st table in the list tb has 1st element in the list ab , 2nd table has 2nd element and so on. The number of elements in both lis开发者_运维技巧t ab and tb are the same.

  4. How will I match the rownames of the tables in the list with the list of elements?


> is.in <- vector(len=length(ab))
> for(i in 1:length(ab) ) { is.in[i] <- ab[i] %in% rownames(tb[[i]])}
> is.in
 #[1]  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

Most of those ab-letters were not in the NMO list so I just checked to see if I were missing some:

 > tb[[8]] N M
O 1 4
M 2 3

But ab[8] = "N" is not in the rownames of tb[[8]]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜