Treat NA as zero only when adding a number

ε祈祈猫儿з 提交于 2019-11-29 09:26:29

问题


When calculating the sum of two data tables, NA+n=NA.

> dt1 <- data.table(Name=c("Joe","Ann"), "1"=c(0,NA), "2"=c(3,NA))
> dt1
   Name  1  2
1:  Joe  0  3
2:  Ann NA NA
> dt2 <- data.table(Name=c("Joe","Ann"), "1"=c(0,NA), "2"=c(2,3))
> dt2
   Name  1 2
1:  Joe  0 2
2:  Ann NA 3
> dtsum  <- rbind(dt1, dt2)[, lapply(.SD, sum), by=Name]
> dtsum
   Name  1  2
1:  Joe  0  5
2:  Ann NA NA

I don't want to substitute all NA's with 0. What I want is NA+NA=NA and NA+n=n to get the following result:

   Name  1  2
1:  Joe  0  5
2:  Ann NA  3

How is this done in R?

UPDATE: removed typo in dt1


回答1:


You can define your own function to act as you want

plus <- function(x) {
 if(all(is.na(x))){
   c(x[0],NA)} else {
   sum(x,na.rm = TRUE)}
 }


rbind(dt1, dt2)[,lapply(.SD, plus), by = Name]



回答2:


dtsum  <- rbind(dt1, dt2)[, lapply(.SD, function(x) ifelse(all(is.na(x)), as.numeric(NA), sum(x, na.rm=T))), by=Name]

(includes @Arun's suggestion) na.rm=TRUE is very useful to remember



来源:https://stackoverflow.com/questions/15057769/treat-na-as-zero-only-when-adding-a-number

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!