【问题标题】:How do I subset a matrix by rownames using a list of row headers如何使用行标题列表按行名对矩阵进行子集化
【发布时间】:2014-04-08 16:26:04
【问题描述】:

我有一个名字列表(mylist)。此名称列表与(大得多的)文件中的某些行名称一致,该文件还包含其他数据列(大文件)。如何提取与“mylist”中的名称匹配的“bigfile”中的行?

【问题讨论】:

    标签: r list submatrix


    【解决方案1】:

    Hadley's page on subsetting in R 是一个值得一看的好地方。

    就问题的具体答案而言,让我们假设您有一个原始的行名列表,您希望将其作为子集,称为“mylist”。您可以执行以下操作:

    index <- row.names(bigfile)  %in% mylist[,1] 
    

    这应该给出一个与大文件中的行数一样长的布尔表达式。

    编辑:您还可以查看following Stackoverflow post which nicely addresses the problem

    【讨论】:

    • 谢谢。我使用了“索引”的东西。这回答了我的问题。
    • 太棒了!将清理答案以反映这一点!
    • 这真是个好地方 :-)
    【解决方案2】:
    # collect the names from the bigfile
    BigFileNames <- colnames(bigfile)
    
    # find the intersect between your list and the list of names from Bigfile
    common <- intersect(BigFileNames, myListNames)
    
    # extract info from dataframe 
    infoFromCommon <- bigfile[,match(common, myListNames)]
    

    【讨论】:

      【解决方案3】:

      在给定行名的情况下提取矩阵的子集。在片段下方,从用户矩阵中提取子集,其中行名与从数据框 activity_data_t 中提取的给定用户列表匹配

      r_users = users[rownames(users) %in% unique(activity_data_t[activity_data_t$role_id == target_latest_role, "user_id"]),]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-01-13
        • 2021-04-29
        • 1970-01-01
        • 2020-04-19
        • 2018-08-07
        • 1970-01-01
        • 2014-02-02
        相关资源
        最近更新 更多