r - merge data frames to eliminate missing observations -


i have 2 data frames. 1 (df1) contains columns , rows of interest, includes missing observations. other (df2) includes values used in place of missing observations, , includes columns , rows @ least 1 na present in df1. merge 2 data sets somehow obtain desired.result.

this seems simple problem solve, drawing blank. cannot merge work. maybe write nested for-loops, have not done yet. tried aggregate few time. little afraid post question, fearing r card might revoked. sorry if duplicate. did search here , google intensively. thank advice. solution in base r preferable.

df1 = read.table(text = "   county year1 year2 year3     aa     10    20   30     bb      1    na    3     cc      5    10   na     dd    100    na  200 ", sep = "", header = true)  df2 = read.table(text = "   county year2 year3     bb      2   na     cc     na   15     dd    150   na ", sep = "", header = true)  desired.result = read.table(text = "   county year1 year2 year3     aa     10    20   30     bb      1     2    3     cc      5    10   15     dd    100   150  200 ", sep = "", header = true) 

aggregate can this:

aggregate(. ~ county,           data=merge(df1, df2, all=true), # merged data, including nas           na.action=na.pass,              # aggregate rows missing values...           fun=sum, na.rm=true)            # ...but instruct "sum" ignore them. ##   county year2 year3 year1 ## 1     aa    20    30    10 ## 2     bb     2     3     1 ## 3     cc    10    15     5 ## 4     dd   150   200   100 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -