I have a data set which looks like the following:
The 'X19' is the row number of another data set. How can I merge these two data sets such that 'FNUMM' will be added to each row appears in 'X19'?
Thanks.
I have a data set which looks like the following:
The 'X19' is the row number of another data set. How can I merge these two data sets such that 'FNUMM' will be added to each row appears in 'X19'?
Thanks.
Try merge(df1, df2, by = 'X19')
, where df1 and df2 are your two data frames.
This is a merge where one of the keys is the rownames
of one dataset. You can do this:
cbind(df1, df2[, "FNUMM"][match(rownames(df1), df2$X19)])
Here is a reproducible example
df1 <- data.frame(ID=c(1L,1L,1L,1L,2L,2L,3L,3L),
var=c(1:8), Smoke=c('No','No','Yes','No','No','No','Yes','No'))
df2 <- data.frame(X19=c(2,5,8), FNUMM=c('a','b','c'))
cbind(df1, df2[, "FNUMM"][match(rownames(df1), df2$X19)])