Search code examples
rsumsummarize

Sum of rows (with NA's) in R


I have a data.frame "data" with the columns "var1" ... "var3". Now, I'd like to calculate a new column "sum" from the three var-columns. Unfortunately, in every row only one variable out of the three has a value:

var1  var2  var3  sum
NA    NA    300   300
20    NA    NA     20
10    NA    NA     10

Do I have to replace the NA's with 0 first in order to compute the sum-column or is there a more elegant way? Thank you!


Solution

  • We can use rowSums

    df1$sum <- rowSums(df1[grep("^var\\d+", names(df1))], na.rm = TRUE)
    

    Also, if there is only a single non-NA element, another option is

    do.call(pmax, c(df1[1:3], na.rm = TRUE))