1

I try to sum 2 columns with some NA. There are a lot of forum questions like my first question: how to sum and ignore NA, but now I do want it to return NA when both columns have NA in a specific row. This is an example:

  df<-data.table(x = c(1,2,NA),
                 y = c(1,NA,NA))
> df
  x  y
  1  1
  2 NA
 NA NA

and I want this:

  x  y final
  1  1   2
  2 NA   2
 NA NA   NA

I've tried the following:

df$sum<-rowSums(df[,c("x", "y")], na.rm=TRUE)
  df$final<-ifelse (is.na(df$x) && is.na(df$y) , NA,
                              ifelse (is.na(df$x) | is.na(df$y), df$sum,
                                      ifelse (!is.na(df$x) && !is.na(df$y), df$sum)))

But this doesn't return what I want.. Could someone help me..?

NOTE: Some have said this is a duplicate for the reason that I ask that NA's are ignored, but those questions do not answer my main question: How should 2 x NAget me NA and not 0

Lotw
  • 423
  • 1
  • 4
  • 15

2 Answers2

4

I used the following. It gives sums even when there are NAs, but returns NA when all sumed elements are NA.

rowSums(df, na.rm = TRUE) * NA ^ (rowSums(!is.na(df)) == 0)
Claudiu Papasteri
  • 2,469
  • 1
  • 17
  • 30
3

Here are two more options:

ifelse(rowSums(is.na(df)) != ncol(df), rowSums(df, na.rm = TRUE), NA)
#[1]  2  2 NA

and

vals <- rowSums(df, na.rm = TRUE)
NA^(vals == 0) * vals 
#[1]  2  2 NA
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213