Bonono Bonono - 3 months ago 7
R Question

Match two columns resulting in a third of longer length

I have two

data.frame
s:

df1 <- data.frame(ID = c(1,2,3,4), Birth.date = c("2015-09-16","2015-09-17","2015-09-18","2015-09-19"))

df2 <- data.frame(ID = c(1,1,2,2,3,3,4,4), value = c("a","b","c","d","e","a","b","c"))


Goal is to add a
Birth.date
column to
df2
, so that each row shows the birth date of each
ID
from
df1
. Resulting in looking like this:

Goal <- data.frame(ID = c(1,1,2,2,3,3,4,4), value = c(a,b,c,d,e,a,b,c)), Birth.date = c("2015-09-16","2015-09-16","2015-09-17","2015-09-17","2015-09-18","2015-09-18","2015-09-19","2015-09-19"))


I tried using
match()
but it gave this:

df2$Birth.Date <- df1[match(df1$ID, df2$ID),2]

df2

ID value Birth.Date
1 1 a 2015-09-16
2 1 b 2015-09-18
3 2 c <NA>
4 2 d <NA>
5 3 e 2015-09-16
6 3 a 2015-09-18
7 4 b <NA>
8 4 c <NA>


Been trying to figure it out for a while now but to no avail. Any help?

Answer

We can use left_join

library(dplyr)
left_join(df2, df1, by = "ID")
#     ID value Birth.date
#1  1     a 2015-09-16
#2  1     b 2015-09-16
#3  2     c 2015-09-17
#4  2     d 2015-09-17
#5  3     e 2015-09-18
#6  3     a 2015-09-18
#7  4     b 2015-09-19
#8  4     c 2015-09-19

If we are using match, the correct option would be to have x as the 'ID' from 'df2' and table with 'ID' from 'df1'

df2$Birth.date <- df1$Birth.date[match(df2$ID, df1$ID)]
Comments