【发布时间】:2014-04-08 16:26:04
【问题描述】:
我有一个名字列表(mylist)。此名称列表与(大得多的)文件中的某些行名称一致,该文件还包含其他数据列(大文件)。如何提取与“mylist”中的名称匹配的“bigfile”中的行?
【问题讨论】:
我有一个名字列表(mylist)。此名称列表与(大得多的)文件中的某些行名称一致,该文件还包含其他数据列(大文件)。如何提取与“mylist”中的名称匹配的“bigfile”中的行?
【问题讨论】:
Hadley's page on subsetting in R 是一个值得一看的好地方。
就问题的具体答案而言,让我们假设您有一个原始的行名列表,您希望将其作为子集,称为“mylist”。您可以执行以下操作:
index <- row.names(bigfile) %in% mylist[,1]
这应该给出一个与大文件中的行数一样长的布尔表达式。
编辑:您还可以查看following Stackoverflow post which nicely addresses the problem。
【讨论】:
# 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)]
【讨论】:
在给定行名的情况下提取矩阵的子集。在片段下方,从用户矩阵中提取子集,其中行名与从数据框 activity_data_t 中提取的给定用户列表匹配
r_users = users[rownames(users) %in% unique(activity_data_t[activity_data_t$role_id == target_latest_role, "user_id"]),]
【讨论】: