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
  • 58,147
  • 10
  • 114
  • 138
Mohamed Rahouma
  • 1,084
  • 9
  • 20
  • 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
  • 254,981
  • 17
  • 203
  • 341
  • Thanks for your valuable input. My actual df has many other columns eg a,b,c, d,e but I need sum of a,b,c provided here only. How I should proceed with your provided code? – Mohamed Rahouma Aug 06 '19 at 17:49
  • Thanks. I used it but I needed to modify it (adding "df$sum<-") to be as follow sum_or_na <- function(x) if (all(is.na(x))) NA else sum(x, na.rm = TRUE) df$sum<- transform(df, sum = apply(df[c("a", "b", "c")], 1, sum_or_na)). Appreciate your help – Mohamed Rahouma Aug 06 '19 at 18:05
  • That is not correct. It would be done like this where `df2` is `df` with the added `sum` column. `df2 <- transform(...)` You could overwrite `df `like this `df <- transform(...)` but normally it is better practice to avoid overwriting as much as possible. – G. Grothendieck Aug 06 '19 at 18:10