Subsetting a matrix by row.names

后端 未结 3 974
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 01:49

I have a matrix with the following row.names:

\"X1\"   \"X5\"   \"X33\"  \"X37\"  \"X52\"  \"X566\"

Now I want to select only the rows whic

相关标签:
3条回答
  • 2020-12-03 02:16

    This also seems to work:

    include_list <- head(read.csv("/Users/histelheim/include_list.csv", header = FALSE))
    include_list <- c(do.call("cbind", include_list)) 
    data[row.names(data) %in% include_list, ]
    
    0 讨论(0)
  • 2020-12-03 02:24

    Just in case someone's include_list is class list (rather than vector), then this is modification of Rich Scriven's answer that works in such case based on this. unlist() should be added:

    m <- matrix(1:30, 6, 5)
    rownames(m) <- c("X1", "X5", "X33", "X37", "X52", "X566")
    include_list <- list("X1", "X5", "X33")
    
    
    m[unlist(include_list), ]
    
    0 讨论(0)
  • 2020-12-03 02:25

    Set up some fake data:

    m <- matrix(1:30, 6, 5)
    rownames(m) <- c("X1", "X5", "X33", "X37", "X52", "X566")
    m
    #      [,1] [,2] [,3] [,4] [,5]
    # X1      1    7   13   19   25
    # X5      2    8   14   20   26
    # X33     3    9   15   21   27
    # X37     4   10   16   22   28
    # X52     5   11   17   23   29
    # X566    6   12   18   24   30
    

    Here it's probably easiest to subset with matrix indexing ([):

    include_list <- c("X1", "X5", "X33")
    m[include_list, ]
    #     [,1] [,2] [,3] [,4] [,5]
    # X1     1    7   13   19   25
    # X5     2    8   14   20   26
    # X33    3    9   15   21   27
    

    Alternative with subset() function:

    subset(m, rownames(m) %in% include_list)
    #      [,1] [,2] [,3] [,4] [,5]
    # X1     1    7   13   19   25
    # X5     2    8   14   20   26
    # X33    3    9   15   21   27
    
    0 讨论(0)
提交回复
热议问题