merge data frames to eliminate missing observations

前端 未结 3 2043
梦谈多话
梦谈多话 2020-12-19 07:43

I have two data frames. One (df1) contains all columns and rows of interest, but includes missing observations. The other (df2) includes values t

3条回答
  •  清歌不尽
    2020-12-19 08:31

    aggregate can do this:

    aggregate(. ~ county,
              data=merge(df1, df2, all=TRUE), # Merged data, including NAs
              na.action=na.pass,              # Aggregate rows with missing values...
              FUN=sum, na.rm=TRUE)            # ...but instruct "sum" to 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
    

提交回复
热议问题