r - 如何自动查找列表的值是否存在于 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{
.....?????????
解决方案
您可以在名为dplyr的 R 包中使用intersect函数,该函数可用于查找不同列、不同列表等之间的共同元素。值得一试。例如,您可以将列读取为向量,然后执行以下操作:
column1 = c(1,2,3,4)
column2 = c(6,5,4,3,2)
intersect(vector_1, vector_2)
现在这个将返回 3 和 4,因为它们在两列中都很常见。
推荐阅读
- c++ - 这个 c++ 素数程序中的运行时错误是什么?
- python - 如何让 cudnn 工作?(初始化失败)
- java - 在 Apache Beam 中写入不同的源
- dart - 任何获取 GoogleCredential 访问令牌的 Flutter 包
- javascript - 如何以百分比显示 HTML 表格数据
- java - Maven - 构建 jar 后文件丢失
- design-patterns - 重构代码以在 Rust 中实现组合的最佳方法是什么?
- php - Php Array Key Exist but it is impossible to access element giving
- sql - JSONB 转数组数据类型
- android - 如果我使用简单的适配器来提供列表视图并且列表视图包含文本视图和按钮,那么我该如何在特定位置执行操作?