Currently I am trying to generate a new sum variable with
mutate(). I want to generate the sums of 10 different variables where row-wise are always different numbers of figures to sum up. So in one row only 2 of 10 variables have summable numbers (The rest is
NA), in other rows there 4 or 6, for example.
I already know that in this kind of data frame it's important to omit
NAs to sum up rows. That's why I wanted to use
na.rm=TRUE, but in
mutate() it's just gonna generate a column named
"na.rm" with all rows showing the content "TRUE".
Now I have already tried the following approaches:
library(dplyr) data %>% rowwise() %>% mutate(sum = sum(a,b,c, na.rm=TRUE))
data %>% mutate(sum = rowSums(., na.rm = TRUE))
None of these approaches works in my case. The sum variable just remains NA in all rows which contain at least one
NA. So I guess the
NAs won't be omitted properly for some reason, even though I put
na.rm on "TRUE".
I have also seen that the operations in the code blocks above just won't do anything. They won't generate a new sum column or change the existing one from the
mutate() operation which won't omit the
I gotta overlook something and I just don't know what.