ignore NA in dplyr row sum

后端 未结 6 2124
自闭症患者
自闭症患者 2020-11-27 17:27

is there an elegant way to handle NA as 0 (na.rm = TRUE) in dplyr?

data <- data.frame(a=c(1,2,3,4), b=c(4,NA,5,6), c=c(7,8,9,NA))

data %>% mutate(sum          


        
6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 17:48

    You could use this:

    library(dplyr)
    data %>% 
      #rowwise will make sure the sum operation will occur on each row
      rowwise() %>% 
      #then a simple sum(..., na.rm=TRUE) is enough to result in what you need
      mutate(sum = sum(a,b,c, na.rm=TRUE))
    

    Output:

    Source: local data frame [4 x 4]
    Groups: 
    
          a     b     c   sum
      (dbl) (dbl) (dbl) (dbl)
    1     1     4     7    12
    2     2    NA     8    10
    3     3     5     9    17
    4     4     6    NA    10
    

提交回复
热议问题