Home > Mobile >  Recode and sum to NA when all values are NA in R
Recode and sum to NA when all values are NA in R

Time:10-05

I need to assign NA when all the columns are empty in summation for each id.

Here is how my sample dataset looks like;

df <- data.frame(id = c(1,2,3),
                 i1 = c(1,NA,0),
                 i2 = c(1,NA,1),
                 i3 = c(1,NA,0),
                 total = c(3,0,1))

> df
  id i1 i2 i3 total
1  1  1  1  1     3
2  2 NA NA NA     0
3  3  0  1  0     1

For the second id the total should be NA instead of 0 because all the values are NA for the second id. How can I change the dataset to below?

  > df1
      id i1 i2 i3 total
    1  1  1  1  1     3
    2  2 NA NA NA     NA
    3  3  0  1  0     1

CodePudding user response:

We could create a condition with if_all in case_when to return NA when all the column values are NA for a row or else do the rowSums with na.rm = TRUE

library(dplyr)
df %>%
   mutate(total = case_when(if_all(i1:i3,  is.na) ~ NA_real_,
        TRUE ~ rowSums(across(i1:i3), na.rm = TRUE)))

-output

  id i1 i2 i3 total
1  1  1  1  1     3
2  2 NA NA NA    NA
3  3  0  1  0     1
  • Related