0

I have a df1:

IDname    Date     Score1    Score2
3         1/20/19   77        79
1         9/23/20   79        76
2         11/2/21   78        79
1         10/20/20  70        74
3         3/3/20    70        74
4         10/20/20  72        75
5         1/1/11    73        76
3         4/20/19   71        79

And a df2:

IDname    Date      Error           
1         9/23/20   1       
2         11/2/21   1             
3         3/3/20    1        
3         4/20/19   0              
5         1/1/11    0
1         10/20/20  0 
3         1/20/19   0 
4         10/20/20  0 

I would like to merge these two columns to create a new df. I would like to match rows based off of BOTH IDName and Date. If the ID name matches, but the dates are different, I would not like to match. Similarly, if the date matches but not the ID, then I would not like to match. The final df3 would look like:

IDname    Date      Error   Score1  Score2     
1         10/20/20  0       70      74        
1         9/23/20   1       79      76
2         11/2/21   1       78      79
3         1/20/19   0       77      79
3         3/3/20    1       70      74
3         4/20/19   0       71      79
4         10/20/20  0       72      75
5         1/1/11    0       73      76
Evan
  • 1,477
  • 1
  • 17
  • 34

1 Answers1

1
library(tidyverse)

Sample data

df1 <-
  tibble(
    IDname = c(3,1,2,1,3,4,5,3),
    Date = c("1/20/19","9/23/20","11/2/21","10/20/20","3/3/20","10/20/20","1/1/21","4/20/19"),
    Score1 = c(77,79,78,70,70,72,73,71),
    Score2 = c(79,76,79,74,74,75,76,79)
  )

df2 <-
  tibble(
    IDname = c(1,2,3,3,5,1,3,4),
    Date = c("9/23/20","11/2/21","3/3/20","4/20/19","1/1/21","10/20/20","1/20/19","10/20/20"),
    Error = c(1,1,1,0,0,0,0,0)
  )

Inner join

With inner join ony matched rows will remain in both tables (df1 and df2), using IDname and Date as reference

enter image description here

Code

df1 %>% 
  inner_join(df2) %>% 
  arrange(IDname,Date)

Result

enter image description here

Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32