Dear Community,
I want to calculate the mean across a specific amount of variables and tried using mutate with rowMeans.
So far, there are two difficulties about it:
a) I get an error that 'x' must be numeric - is that because of NA for missings? How to deal with it?
b) In other versions with mutate the new var is shown in the tibble, but not added/saved to the existing dataset? How can I really add and keep it?
I used this code:
ssco_str <- c("s1abc", "s1cde", "s1efg")
fhprofs %>% mutate(ssco_meanall = rowMeans(fhprofs, na.rm=TRUE),
ssco_strmean = rowMeans(select(fhprofs, ssco_str, na.rm=TRUE)))
The tibble I get from another code looked like this (mean in output of the tibble but not saved in dataframe):
fhprofs %>%
select (s1abc, s1cde, s1efg ) %>%
mutate(
ssco_strmean=(s1abc+ s1cde,+s1efg)/3
)
A tibble: 60 x 4
s1strgarbm s1strgarbo s1strgbkol ssco_strmean
<dbl+lbl> <dbl+lbl> <dbl+lbl> <dbl>
1 5 [trifft voll und ganz zu] 5 [trifft voll und ganz zu] 5 [trifft voll und ganz zu] 5
2 5 [trifft voll und ganz zu] 5 [trifft voll und ganz zu] 5 [trifft voll und ganz zu] 5
3 4 [trifft voll und ganz zu] 5 [trifft voll und ganz zu] 5 [trifft voll und ganz zu] 4.67
4 5 [trifft voll und ganz zu] 1 [trifft voll und ganz zu] 5 [trifft voll und ganz zu] 3.67