Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
比较R中的2个数据帧:在df2$V2中搜索df1$V2中的字符串,并在df2$V1中返回字符串_R_String_Dataframe_Matching - Fatal编程技术网

比较R中的2个数据帧:在df2$V2中搜索df1$V2中的字符串,并在df2$V1中返回字符串

比较R中的2个数据帧:在df2$V2中搜索df1$V2中的字符串,并在df2$V1中返回字符串,r,string,dataframe,matching,R,String,Dataframe,Matching,我试图比较R中的两个数据帧: Keggs <- c("K001", "K002", "K003", "K004", "K005", "K006", "K007", "K008") names <- c("Acaryochloris", "Proteobacteria", "Parvibaculum", "Alphaproteobacteria", "Rhodospirillum", "Magnetospirillum", "Coraliomargarita", "Bacteria")

我试图比较R中的两个数据帧:

Keggs <- c("K001", "K002", "K003", "K004", "K005", "K006", "K007", "K008")
names <- c("Acaryochloris", "Proteobacteria", "Parvibaculum", "Alphaproteobacteria", "Rhodospirillum", "Magnetospirillum", "Coraliomargarita", "Bacteria")
family <- c("Proteos", "Cyanobacteria", "Rhizo", "Nostocales", "Bacteroidetes")
species <- c("Alphaproteobacteria", "Purrsia", "Parvibaculum", "Chico", "Rhodospirillum")
res <- data.frame(Keggs, names)
result <- data.frame(family, species) 
我搜索了如何比较R中的data.frames,找到的最接近的是:

但是它返回T/F,res df是2列

然而,在我的搜索中,我在base R中使用了
match()
merge()
函数;我使用的“res”df是11000000行,而我的“result”df小于1000行。在匹配文档中,它指出:
match(x,表…)
在表下面:“不支持长向量”,所以,我不认为match()或merge()方法(由于我实际的df的大小)是最优雅的。我试过打圈,但我的圈技有限,所以我放弃了

如果能深入了解这个难题,我将不胜感激

提前谢谢大家,,
Purrsia

您可以尝试以下功能:

df3 <- res %>% 
  inner_join(result, by = c("names" = "species")) %>%
  select(Keggs, family)

我们可以使用
data.table

library(data.table)
na.omit(setDT(res)[result, on = c("names" = "species")])[, names := NULL][]
#   Keggs        family
#1:  K004       Proteos
#2:  K003         Rhizo
#3:  K005 Bacteroidetes

你真的试过
match
电话吗?1e7可能看起来很大,但我认为您可能误解了R的“长向量”是什么。在控制台上键入
news()
,向下滚动到“长向量”,然后阅读。您是否尝试过
merge(res,result,by.x=“name”,by.y=“species”)
?r2evens:首先,感谢您的新闻()。我不知道这件事。很好的工具。我确实读过:2^31。所以,我很有我的极限。很抱歉,我确实尝试了以下命令:
matched
na.ommit
是一个非常低效的函数,您可以只指定
,nomatch=0L
,起初我一直收到一个错误,即找不到%>%函数,但在这个站点上搜索之后,我了解到我必须附加
dplyr
包。它工作得很好。谢谢你,阿拉米斯。(
管道
操作员
%
主要来自
magrittr
软件包,但是
tidyverse
方便地包括
dplyr
和基本管道操作员。这是很好的信息。学会了。非常感谢!:)
  Keggs        family
1  K003         Rhizo
2  K004       Proteos
3  K005 Bacteroidetes
library(data.table)
na.omit(setDT(res)[result, on = c("names" = "species")])[, names := NULL][]
#   Keggs        family
#1:  K004       Proteos
#2:  K003         Rhizo
#3:  K005 Bacteroidetes