I have two data frames (df1, df2). I want to fill in the AGE and SEX values from df1 to df2 due to the same identifier between them. I tried several ways to use for-loop and check if the object id matches between two data frames, but I failed. The result should be the same as in df3. I have a huge data set, so I want part of the code in R to be able to do this easily. I would be grateful for your help in this. Thank.
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
match lapply . [[ ID , .
match
lapply
[[
ID
nm <- c("AGE", "SEX") df2[nm] <- lapply(nm, function(x) df1[[x]][match(df2$ID, df1$ID)]) df2 # ID AGE SEX Conc # 1 90901 39 0 5.00 # 2 90901 39 0 10.00 # 3 90901 39 0 15.00 # 4 90903 40 1 30.00 # 5 90903 40 1 5.00 # 6 90902 28 0 2.45 # 7 90902 28 0 51.00 # 8 90902 28 0 1.00 # 9 70905 NA NA 0.50
, , merge.
merge
merge(df1, df2, by = "id"). . , df2 .
merge(df1, df2, by = "id")
df2$AGE <- NULL df2$SEX <- NULL df3 <- merge(df1, df2, by = "id")
df2, df1, :
df2 <- subset(df2, select = -c(AGE,SEX) ) df3 <- merge(df1, df2, by = "id", all.y = TRUE)
merge ( r) , ?merge() r.
?merge()