将第一个表匹配到第二个表,并使用R将第二个表中的相应信息复制到第一个表中

将第一个表匹配到第二个表,并使用R将第二个表中的相应信息复制到第一个表中,r,join,merge,R,Join,Merge,我想从参考表的列物种中提取信息,并将其添加到表1的列物种中 a <- c(1:10) b <- c(2001,NA,NA,2004,2005,NA,2007,NA,2009,2010) c <- c('A','B',NA,'D','E','F','G','H',NA,NA) table1 <- data.frame(serial=a, id=b, species=c) e <- c(2001:2010) f <- c('A','B','C','D','E',

我想从参考表的列物种中提取信息,并将其添加到表1的列物种中

a <- c(1:10)
b <- c(2001,NA,NA,2004,2005,NA,2007,NA,2009,2010)
c <- c('A','B',NA,'D','E','F','G','H',NA,NA)
table1 <- data.frame(serial=a, id=b, species=c)

e <- c(2001:2010)
f <- c('A','B','C','D','E','F','G','H','I','J')
reference_table <- data.frame(id=e, species=f)
输出2-其中列id第2行、第6行和第8行分别得到20022006和2008

#table1
#Serial  id  species
# 1     2001   A
# 2     2002   B
# 3     NA     NA
# 4     2004   D
# 5     2005   E
# 6     2006   F
# 7     2007   G
# 8     2008   H
# 9     2009   I
# 10    2010   J
我用的是MySQL

UPDATE table1 AS a JOIN reference_table AS b ON a.species = b.species SET a.id = b.id

UPDATE table1 AS a JOIN reference_table AS b ON a.id= b.id SET a.species  = b.species 

您是否尝试过
合并()
?你到底遇到了什么问题?@MrFlick我原来的表1是9460x47,当我使用merge(x=table1,y=reference\u table,by=“ID”,all=TRUE)时,我得到了1000万行的输出swell,你可能不想要
all=t
。您需要
all.x=T
all.y=T
进行左或右连接。@MrFlick现在我使用了all.x=T,它比原始表多了1000行,如果是all.y=T,则比原始表少了1600行。您是否碰巧有重复的?
UPDATE table1 AS a JOIN reference_table AS b ON a.species = b.species SET a.id = b.id

UPDATE table1 AS a JOIN reference_table AS b ON a.id= b.id SET a.species  = b.species