答案 0 :(得分:1)
如果您想要在两个数据集中按列“ID”合并,这将适用于您。
merge(x = TABLE1, y = TABLE2[ , c("Name", "Date", "ID")], by = "ID", all=TRUE)
我在合并时子设置第二个数据集。如果您要按Name
,Date
和ID
列合并,则可以执行以下操作:
merge(x = TABLE1, y = TABLE2[ , c("Name", "Date", "ID")], by = c("Name", "Date", "ID"), all=TRUE)
#Output
Name Date ID Price
1 Green <NA> KF3902 NA
2 Pink <NA> F43346 NA
3 <NA> 2017-08-04 AMA 2
4 <NA> 2017-09-29 <NA> 33
答案 1 :(得分:1)
以下是dplyr
的解决方案:
library(dplyr)
full_join(table1, table2,
by = c("Name", "Date", "ID")) %>%
select(-Cost, -PNL) %>%
rename(Item = ID)
<强>结果:强>
Name Date Price Item
1 <NA> 2017-09-29 33 <NA>
2 <NA> 2017-08-04 2 AMA
3 Pink <NA> NA F43346
4 Green <NA> NA KF3902
备注:强>
我的解决方案对数据集table1 and
table2做了一些假设。例如,Date
是否已转换为日期变量,Name
和ID
变量是否为字符类型而不是因子。因此,一旦OP提供数据,我将调整我的解决方案。
数据:强>
table1 = data.frame(Name = as.character(c(NA, NA)),
Date = as.Date(c("2017-9-29", "2017-8-4")),
Price = c(33, 2),
ID = c(NA, "AMA"),
stringsAsFactors = FALSE)
table2 = data.frame(Name = c("Pink", "Green"),
Cost = c(25, 876),
PNL = c(11, 252),
Date = as.Date(c(NA, NA)),
ID = c("F43346", "KF3902"),
stringsAsFactors = FALSE)