Sum of two Columns of Data Frame with NA Values

后端 未结 5 620
余生分开走
余生分开走 2020-12-05 10:33

I have a data frame with some NA values. I need the sum of two of the columns. If a value is NA, I need to treat it as zero.

a  b c d
1  2 3 4
5 NA 7 8
         


        
5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-05 10:39

    dat$e <- rowSums(dat[,c("b", "c")], na.rm=TRUE)
    dat
    #   a  b c d e
    # 1 1  2 3 4 5
    # 2 5 NA 7 8 7
    

提交回复
热议问题