To simplify, I have two similar dataframes:
df1 <- data.frame(a=seq(0, 100, by=5), b=rep(NA, times = 21))
df2 <- data.frame(a=seq(0, 100, by=10), b=seq(0, 10, by=1))
I wnat to assign df2$b values to df1$b only when the a-values match, ie,
intersect(df1$a,df2$a)
This will occur at a-values of 0,10,20,30, etc, for which the b-values
0,1,2,3, etc should be copied from df2$b to df1$b.
I am a novice r programmer and cannot see how to do this using only base-r code,
so I can understand the mechanics of the match-test and assignment.
I tried:
df1$b[intersect(df1$a, df2$a)] <- df2$b[intersect(df1$a, df2$a)]
df1$b should then be: 0 NA 1 NA 2 NA 3 NA , etc
Thanks much
Marvin