Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sum non NA elements only, but if all NA then return NA

Tags:

r

data.table

I think I already got really good answers on the comments, but I will rephrase the question for future reference.

I am trying to sum by groups using data.table. The problem is that some groups only have NA. For these groups I would like the sum to return NA. However, if there is one group that has one value different that NA, I would like to get the sum of the non-NA values.

A <- data.table(col1= c('A','A','B','B','C','C'),  
                col2= c(NA,NA,2,3,NA,4))

This without adding the argument na.rm = T, group C returns NA when it should return 4.

A[, sum(col2), by = .(col1)]
   col1 V1
1:    A NA
2:    B  5
3:    C NA

However, adding na.rm = T returns 0 in group A when it should return NA.

A[, sum(col2, na.rm = T), by = .(col1)]
   col1 V1
1:    A  0
2:    B  5
3:    C  4

The approach that i like the best is the one that sandipan suggested in the comments, which is akin to the function I wrote below:

ifelse(all(is.na(col2)), NA, sum(col2, na.rm = T)

I created a function to get around it, but I am not sure whether there is an already built-in way to get around this:

sum.na <- function(df){

  if (all(is.na(df))){

    suma <- NA
  }  
  else {    
    suma <- sum(df, na.rm = T)
  }

  return(suma)
}
like image 760
dleal Avatar asked Jan 04 '17 17:01

dleal


1 Answers

Following the suggestions from other users, I will post the answer to my question. The solution was provided by @sandipan in the comments above:

As noted in the question, if you need to sum the values of one column which contains NAs,there are two good approaches:

1) using ifelse:

A[, (ifelse(all(is.na(col2)), col2[NA_integer_], sum(col2, na.rm = T))), 
  by = .(col1)]

2) define a function as suggested by @Frank:

suma = function(x) if (all(is.na(x))) x[NA_integer_] else sum(x, na.rm = TRUE)

A[, suma(col2), by = .(col1)]

Note that I added NA_integer_ as @Frank pointed out because I kept getting errors about the types.

like image 70
dleal Avatar answered Sep 28 '22 06:09

dleal