尝试在R中合并两个数据框时遇到问题,需要您的帮助。 假设我有以下数据框:
> Data_A
Code year score
A 1991 1
A 1992 2
A 1993 3
B 1991 3
B 1993 7
> Data_B
Code year l.score
A 1991 NA
A 1992 1
A 1993 2
A 1994 3
B 1991 NA
B 1992 3
B 1993 NA
B 1994 7
合并后的欲望结果应该是这样的:
> Data_merge
Code year score l.score
A 1991 1 NA
A 1992 2 1
A 1993 3 2
B 1991 3 NA
B 1993 7 NA
这意味着在合并这些数据框时,将保留一个共享列(在这种情况下,为Data_A的“代码”和“年份”)。我尝试了merge(Data_A, Data_B, all = FALSE)
,但没有成功。有人知道吗谢谢阅读!
答案 0 :(得分:0)
library(dplyr)
Data_A %>%
left_join(Data_B, by = c('Code', 'year'))
Code year score l.score
1 A 1991 1 NA
2 A 1992 2 1
3 A 1993 3 2
4 B 1991 3 NA
5 B 1993 7 NA
答案 1 :(得分:0)
您似乎可以使用自己的解决方案(我正在使用R 3.6.1
),但不知道为什么不能这样做
> merge(Data_A, Data_B, all = FALSE)
Code year score X1.score
1 A 1991 1 NA
2 A 1992 2 1
3 A 1993 3 2
4 B 1991 3 NA
5 B 1993 7 NA
数据
Data_A <- structure(list(Code = structure(c(1L, 1L, 1L, 2L, 2L), .Label = c("A",
"B"), class = "factor"), year = c(1991L, 1992L, 1993L, 1991L,
1993L), score = c(1L, 2L, 3L, 3L, 7L)), class = "data.frame", row.names = c(NA,
-5L))
Data_B <- structure(list(Code = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L), .Label = c("A", "B"), class = "factor"), year = c(1991L,
1992L, 1993L, 1994L, 1991L, 1992L, 1993L, 1994L), X1.score = c(NA,
1L, 2L, 3L, NA, 3L, NA, 7L)), class = "data.frame", row.names = c(NA,
-8L))