I am working with two dataframes that have some shared columns but also different columns. The first one has date and ID. The second one has ID, sex, release type (rt), and release year (ry). Here are two dummy dataframes.
library(tidyverse)
#First dataframe
date = c("2015-05-01","2015-05-04","2015-05-05","2015-07-01","2015-07-02","2015-07-05","2015-07-06",
"2015-05-01","2015-05-04","2015-05-05","2015-05-27","2015-05-28","2015-06-05","2015-06-06",
"2015-05-01","2015-05-02","2015-05-03","2015-05-04","2015-05-05","2015-05-06","2015-05-07")
ID = c("0-10","0-10","0-10","0-10","0-10","0-10","0-10",
"0-2","0-2","0-2","0-2","0-2","0-2","0-2",
"0-8","0-8","0-8","0-8","0-8","0-8","0-8")
df1 = data.frame(date,ID)
df1$date = as.Date(df1$date)
Second dataframe
ID = c("0-10", "0-2","0-8","0-4","5-0")
sex = c("M","F","Unknown","F","F")
rt = c("S", "S", "H", "H", "S")
ry = c("2015", "2016","2015", "2015", "2016")
df2 <- data.frame(ID, sex, rt, ry)
df2$sex = as.factor(df2$sex)
What I need is, for EACH entry in dataframe one (df1), info on sex to be automatically filled in from dataframe two (df2) without merging other columns in the second dataframe. I.e., I only need a sex column in the df1.
I have tried the codes for the dplyr package in this thread How to join (merge) data frames (inner, outer, left, right) but when I tried it, it merged all the columns not just the one I needed.
Any help is appreciated. (I also prefer using the dplyr package). Thank you.