I have two data frames. One (df1) contains all columns and rows of interest, but includes missing observations. The other (df2) includes values t
Another option unsing reshape2 and working in the long format :
library(reshape2)
## reshape to long format
df1.m <- melt(df1)
df2.m <- melt(df2)
## get common values
idx <- df1.m$county %in% df2.m$county &
df1.m$variable%in% df2.m$variable
## replace NA values
df1.m[idx,]$value <- ifelse(is.na(df1.m[idx,]$value),
df2.m$value ,
df1.m[idx,]$value)
## get the wide format
dcast(data=df1.m,county~variable)
county year1 year2 year3
1 aa 10 20 30
2 bb 1 2 3
3 cc 5 10 15
4 dd 100 150 200