I have some variables that I wanted to add together but there are missing observations in some of them and when adding together, it will make the whole row with one or more missing as missing. For example, suppose I have the following with the last column as my expectation
df <- matrix(c(23, NA, 56, NA, NA, 43, 67, NA, 11, 10, 18, 39), byrow = T, nrow = 3)
colnames(df)<- c("X", "y", "z", "sum")
df
X y z sum
[1,] 23 NA 56 NA
[2,] NA 43 67 NA
[3,] 11 10 18 39
Here is my expectation
df2 <- matrix(c(23, NA, 56, 79,
NA, 43, 67, 110,
11, 10, 18, 39), byrow = T, nrow = 3)
colnames(df2)<- c("X", "Y", "Z", "sum")
df2
X Y Z sum
[1,] 23 NA 56 79
[2,] NA 43 67 110
[3,] 11 10 18 39
How can I get this result?
I am using R version 3.6 on Window 10.