ignore NA in dplyr row sum

后端 未结 6 2108
自闭症患者
自闭症患者 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:51

    Or we can replace NA with 0 and then use the OP's code

    data %>% 
       mutate_each(funs(replace(., which(is.na(.)), 0))) %>%
       mutate(Sum= a+b+c)
       #or as @Frank mentioned
       #mutate(Sum = Reduce(`+`, .))
    

    Based on the benchmarks using @Steven Beaupré data, it seems to be efficient as well.

提交回复
热议问题