Hello everybody!
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 NA
s 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))
Source: r - ignore NA in dplyr row sum - Stack Overflow
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 NA
s 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 NA
s.
I gotta overlook something and I just don't know what.