首页 > 解决方案 > 如何自动查找列表的值是否存在于 R 的另一列数据框中

问题描述

在现有的 csv 中,有一列包含以下代码之一,即每一行的外膜 [GO:0019867]。我想在 csv 中添加一列,为每一行(即 OuterMembrane)提供一个类别。所以我添加了一个空列,我想制作这个列表,以便在将代码引用到 csv 时自动添加一般类别。(不包括所有编码)

categ <- list(OuterMembrane = c("outer membrane [GO:0019867]","cell outer membrane [GO:0009279]", "integral component of membrane [GO:0016021]", "membrane [GO:0016020]"),
              Cytoplasmic =c("ribosome [GO:0005840]", "cytoplasm [GO:0005737]"),
              Extracellular=c(),
              InnerMembrane=c("plasma membrane [GO:0005886]", "membrane [GO:0016020]"),
              Periplasmic=c("periplasmic space [GO:0042597]"),
              CellWall=c(),
              Vacuole=c(),
              Lipoproteins=c())


csv1 <- csv1%>%
  add_column("Subcellular Localization" = NA)

for (row in (categ)){ 
   if row(categ) %in% csv1{

.....?????????

标签: rcsvfor-loopif-statementbioinformatics

解决方案


您可以在名为dplyr的 R 包中使用intersect函数,该函数可用于查找不同列、不同列表等之间的共同元素。值得一试。例如,您可以将列读取为向量,然后执行以下操作: column1 = c(1,2,3,4) column2 = c(6,5,4,3,2) intersect(vector_1, vector_2) 现在这个将返回 3 和 4,因为它们在两列中都很常见。





推荐阅读