简体   繁体   中英

Recode and sum to NA when all values are NA in R

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM