data:
head(well_being_df2)
# A tibble: 6 x 70
Age Gender EmploymentStatus PWI1 PWI2 PWI3 PWI4 PWI5 PWI6 PWI7 Personality1 Personality2 Personality3
<dbl> <dbl+l> <dbl+lbl> <dbl+> <dbl+> <dbl+> <dbl+> <dbl> <dbl> <dbl> <dbl+lbl> <dbl+lbl>
I am selecting a subset of columns and trying to mutate them. I have played around with the solution provided here but I am getting various errors. I am trying to select the PWI columns, then mutate with rowSums to a new variable called PWI_Index.
This works:
rowSums(select(well_being_df2, contains("PWI")))
[1] 50 32 48 32 58 52 41 51 49 37 50 53 58 47....
[38] 58 60 63 60 63 56 43 30 45 53 45 44 57 55....
[75] 50 55 57 58 57 58 58 58 62 62 44 59 58....
But then when I try to mutate:
mutate(well_being_df2, x = rowSums(select(well_being_df2,
contains("PWI"))))
Which outputs/selects the entire set of columns not the "PWI" columns. Example:
# A tibble: 169 x 71
Age Gender EmploymentStatus PWI1 PWI2 PWI3 PWI4 PWI5 PWI6 PWI7 Personality1 Personality2 Personality3
<dbl> <dbl+l> <dbl+lbl> <dbl+> <dbl+> <dbl+> <dbl> <dbl> <dbl> <dbl> <dbl+lbl> <dbl+lbl> <dbl+lbl>
1 22 2 3 8 8 6 8 8 6 6 1 1 1
2 20 2 1 4 6 1 8 8 4 1 4 5 4
It selects the entire dataframe instead of the selected rowSums of "PWI". Using [.4:10] doesnt work either. Any other solution and I am getting the following error:
select(well_being_df2[.4:10]) %>%
mutate(PWI_Index = rowSums(.)) %>% left_join(well_being_df2)
Error: Column indexes must be integer, not 0.11, 1.11,...
Plus working through previous examples with:
well_being_df2 %>%
mutate(x = rowSums(select(., contains("PWI")))) %>%
head()
And it takes the entire set of columns like before.