问题描述
我有
mydf1 <- data.frame(ID = c(1,2,3,4,5), color = c("red", NA, NA, NA, "green"), name = c("tom", "dick", "harry", "steve", "mike"))
mydf2 <- data.frame(ID = c(1,2,99), color = c("red", "orange", "yellow"), name = c("tom", "dick", "Aaron"))
对于任何ID和名称都匹配的行,我想用mydf2中的相应颜色更新mydf1 $ color.所需的输出将是将第2行中的颜色更新为橙色,而其余颜色保持不变:
I would like to update mydf1$color with the corresponding color from mydf2 for any rows that match on both ID and name. The desired output would be to update the color in row 2 to orange and leave the rest as is:
ID color name
1 1 red tom
2 2 orange dick
3 3 <NA> harry
4 4 <NA> steve
5 5 green mike
我尝试了非对称合并的解决方案,如先前的文章中所述,但是在mydf1中获得了我某些字段的不希望有的覆盖.然后,我尝试按照另一篇文章中的建议使用match,但是收到错误消息.不知道为什么匹配条件不起作用.
I tried solutions with asymmetric merging as in some previous posts, but obtained undesired overwriting of some of my fields in mydf1. I then tried using match as suggested in another post but received an error. Not sure why the match condition is not working.
mydf1$color <- mydf2$color[match(mydf1[c("ID", "name")], mydf2[c("ID", "name")])]
推荐答案
我们可以在和'name'列上使用
data.table
上的连接.并通过赋值(
:=
)
library(data.table)
setDT(mydf1)[mydf2, color := i.color, on = .(ID, name)]
mydf1
# ID color name
#1: 1 red tom
#2: 2 orange dick
#3: 3 <NA> harry
#4: 4 <NA> steve
#5: 5 green mike
match
适用于 vector/matrix
,不适用于 data.frame
.如果需要使用 match
,则粘贴
每个数据集中的'ID','name'并进行 match
match
works on vector/matrix
and not on data.frame
. If we need to use match
, then paste
the 'ID', 'name' from each datasets and do a match
i1 <- match(paste(mydf1$ID, mydf1$name), paste(mydf2$ID, mydf2$name), nomatch = 0)
或使用 tidyverse
library(dplyr)
mydf1 %>%
left_join(mydf2, by = c("ID", "name")) %>%
transmute(ID, name, color = coalesce(as.character(color.x),
as.character(color.y)))
# ID name color
#1 1 tom red
#2 2 dick orange
#3 3 harry <NA>
#4 4 steve <NA>
#5 5 mike green
这篇关于R:根据来自另一个数据框的匹配行更新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!