根据rowname过滤表

时间:2021-09-28 22:21:25
  1. I do have a list of elements in ab,

    我在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,

    在其他列表tb中,是一个2 * 2表的列表,如,

    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 list ab and tb are the same.

    我们只需要过滤那些列表tb中第一个表的rowname在列表ab中有第一个元素的表,第二个表有第二个元素,依此类推。列表ab和tb中的元素数量相同。

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

    如何将列表中表的rownames与元素列表相匹配?

1 个解决方案

#1


1  

> 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:

大多数这些ab字母不在NMO列表中,所以我只是检查一下我是否遗漏了一些:

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

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

但ab [8] =“N”不在tb [[8]]的rownames中

#1


1  

> 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:

大多数这些ab字母不在NMO列表中,所以我只是检查一下我是否遗漏了一些:

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

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

但ab [8] =“N”不在tb [[8]]的rownames中