I am trying to summarize across columns where NA is treated as 0 so long as at least one column has a numerical value, but return NA if both are NA.
I’m trying to call rowSums(x, na.rm = TRUE) but have it return NA when all rows are NA. For example,
# Data example
a <- c(5, NA, NA)
b <- c(6, 6, NA)
df <- data.frame(a, b)
# RowSums
df$c <- rowSums(df[,c("a","b")], na.rm=T)
head(df)
Gives me:
a b c
1 5 6 11
2 NA 6 6
3 NA NA 0
What I really want is:
a b c
1 5 6 11
2 NA 6 6
3 NA NA NA
How would I go about this?
>Solution :
Check if all values are NA:
df$c <- ifelse(rowSums(is.na(df)) == ncol(df), NA, rowSums(df, na.rm = TRUE))
df
# a b c
# 1 5 6 11
# 2 NA 6 6
# 3 NA NA NA