按两个条件合并两个数据框

Merge two dataframes by two conditions

我想根据两个条件(名称和日期)合并两个数据框。如果找不到匹配项,我希望它 return 为 NA。

df1:

Date      Name  Value1   Value2 
2009-03   A     30       456
2009-04   A     33       346
2009-05   A     50       856
2009-03   B     80       44
2009-04   B     34       665
2009-03   C     35       756
2009-04   D     64       66

df2:

Name  Date      ValueX
A     2009-03   34
A     2009-04   466
A     2009-05   55
B     2009-03   65
B     2009-04   568
C     2009-03   56
C     2009-04   676

我希望合并看起来像这样:

Date      Name  Value1   Value2 **ValueX**
2009-03   A     30       456    34
2009-04   A     33       346    466
2009-05   A     50       856    55
2009-03   B     80       44     65
2009-04   B     34       665    568
2009-03   C     35       756    56
2009-04   D     64       66     NA

我尝试了以下方法:

df3 <- left_join(df1, df2, by = c("Date" = "Date", "Name" = "Name"), df1$ValueX)
df3 <- df1
df3$ValueX <- ifelse(
  is.na(match(paste(df1$Date, df1$Name), paste(df2$Date, df2$Name))), 
  df2$ValueX
)
df3 = merge(df1, df2, by.x=c("Date", "Name"), by.y=c("Date", "Name"), all.x=TRUE)

关于如何解决这个问题有什么建议吗?

试试这个:

library(tidyverse)
df3 <- df1 %>% left_join(df2, by= c("Date","Name"))

您可以在 data.table

中尝试
library(data.table)
setDT(df1, key = c("Date","Name"))
setDT(df2, key = c("Name", "Date"))
merge(df1, df2, all.x = TRUE)
#returns
      Date Name Value1 Value2 ValueX
1: 2009-03    A     30    456     34
2: 2009-03    B     80     44     65
3: 2009-03    C     35    756     56
4: 2009-04    A     33    346    466
5: 2009-04    B     34    665    568
6: 2009-04    D     64     66     NA
7: 2009-05    A     50    856     55