我有两个数据集如下 df1:
ID name
1
1
2
2
3
3
3
df2:
ID var1 name
1 54 aaa
1 12 aaa
2 55 bbb
2 87 bbb
3 1 ccc
3 2 ccc
3 12 ccc
3 14 ccc
3 16 ccc
...
我想使用df2填充df1数据中的nave变量。 我该怎么做?
答案 0 :(得分:1)
以下是快速试用。
df1 <- data.frame(ID = c(1,2,3))
df2 <- data.frame(ID = c(1,2,2), name=c('aaa','bbb','bbb'))
# get unique ID and name pairs
df2 <- unique(df2)
# merge that keep all x rows (or left outer join by df1)
merge(df1, df2, by="ID", all=TRUE)
ID name
1 1 aaa
2 2 bbb
3 3 <NA>
## Update
df1 <- data.frame(ID = c(1,1,2,2,3,3,3))
names <- data.frame(ID = c(1,2,3), names=c('aaa','bbb','ccc'))
df2 <- merge(data.frame(ID = c(1,1,2,2,3,3,3,3)), names, by="ID")
df2 <- unique(df2)
merge(df1, df2, by="ID", all.x = TRUE)