I have two dataframes (df1, df2). I want to fill in the AGE and SEX values from df1 to df2 conditioned on having the same ID between the two. I tried several ways using for-loop and checking subject ID match between the two data frame but I failed. The result should be as in the df3. I have a huge dataset, so I want a piece of code in R that can do this easily. I would appreciate your assistance in this. Thank you.
df1:
ID AGE SEX
90901 39 0
90902 28 0
90903 40 1
df2:
ID AGE SEX Conc
90901 NA NA 5
90901 NA NA 10
90901 NA NA 15
90903 NA NA 30
90903 NA NA 5
90902 NA NA 2.45
90902 NA NA 51
90902 NA NA 1
70905 NA NA 0.5
result:
df3:
ID AGE SEX Conc
90901 39 0 5
90901 39 0 10
90901 39 0 15
90903 40 1 30
90903 40 1 5
90902 28 1 2.45
90902 28 0 51
90902 28 0 1
70905 NA NA 0.5