Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Conditional summarize across a row

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading