0

I have 2 data set that look like this

#set1:
DOB.data <- structure(list(name = c("A", "B", "C"), DOB = c("13/12/2000", 
"13/12/2001", "13/12/2002")), class = "data.frame", row.names = c(NA, 
-3L))

#set2:
DOB.data2<-structure(list(name = c("A", "B", "C", "DD"), x = c("1", "2", "3","4")), class = "data.frame", row.names = c(NA, 
-3L))

How do I match DOB column into the other data set based on their name?

Say DOB.data will match the name and left the other that doesn't have matching data in NA

OMG C
  • 97
  • 5

2 Answers2

0

do you mean

DOB.data %>%
  full_join(DOB.data2, by = "name")

  name        DOB x
1    A 13/12/2000 1
2    B 13/12/2001 2
3    C 13/12/2002 3
4   DD       <NA> 4
Park
  • 14,771
  • 6
  • 10
  • 29
0

set2 should have row.names = c(NA, -4L)), not row.names = c(NA, -3L))

not sure what do you want to achieve, but as @Park said, you should take a look at the dplyr *_join functions:

library(dplyr)

new1 <- inner_join(DOB.data, DOB.data2, by = "name") # in new dataset, leave only names which exist in both DOB.data and DOB.data2 and include unique columns from both datasets

new2 <- left_join(DOB.data, DOB.data2, by = "name") # in new dataset, leave only names which exist in DOB.data and DOB.data2 and include unique columns from both datasets, check out the right_join also

new3 <- full_join(DOB.data, DOB.data2, by = "name") # in new dataset include all names from both DOB.data and DOB.data2 and include unique columns from both datasets

gss
  • 1,334
  • 6
  • 11