Set NA to 0 in R

后端 未结 4 1674
心在旅途
心在旅途 2020-11-29 03:17

After merging a dataframe with another im left with random NA\'s for the occasional row. I\'d like to set these NA\'s to 0 so I can perform calculations with them.

4条回答
  •  心在旅途
    2020-11-29 03:44

    To add to James's example, it seems you always have to create an intermediate when performing calculations on NA-containing data frames.

    For instance, adding two columns (A and B) together from a data frame dfr:

    temp.df <- data.frame(dfr) # copy the original
    temp.df[is.na(temp.df)] <- 0
    dfr$C <- temp.df$A + temp.df$B # or any other calculation
    remove('temp.df')
    

    When I do this I throw away the intermediate afterwards with remove/rm.

提交回复
热议问题