0

I am trying to do rowSums but I got zero for the last row and I need it to be "NA".

My df is

  a  b   c  sum
1 1  4   7   12
2 2  NA  8   10
3 3  5   NA  8
4 NA NA  NA  NA

I used this code based on this link; Sum of two Columns of Data Frame with NA Values

df$sum<-rowSums(df[,c("a", "b", "c")], na.rm=T)

Any advice will be greatly appreciated

Jilber Urbina
  • 53,125
  • 10
  • 108
  • 134
Mohamed Rahouma
  • 803
  • 7
  • 15
  • 2
    `replace(rowSums(d, na.rm = TRUE), rowSums(is.na(d)) == NCOL(d), NA)` – d.b Aug 06 '19 at 17:03
  • Similar to @d.b answer: ```replace(rowSums(df1, na.rm = TRUE), !rowSums(!is.na(df1)), NA)``` – M-- Aug 06 '19 at 17:14

1 Answers1

0

For each row check if it is all NA and if so return NA; otherwise, apply sum. We have selected columns a, b and c even though that is all the columns because the poster indicated that there might be additional ones.

sum_or_na <- function(x) if (all(is.na(x))) NA else sum(x, na.rm = TRUE)
transform(df, sum = apply(df[c("a", "b", "c")], 1, sum_or_na))

giving:

   a  b  c sum
1  1  4  7  12
2  2 NA  8  10
3  3  5 NA   8
4 NA NA NA  NA

Note

df in reproducible form is assumed to be:

df <- structure(list(a = c(1L, 2L, 3L, NA), b = c(4L, NA, 5L, NA), 
    c = c(7L, 8L, NA, NA)), 
    row.names = c("1", "2", "3", "4"), class = "data.frame")
G. Grothendieck
  • 233,926
  • 16
  • 195
  • 321