-2

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.

psych.tek
  • 25
  • 5

1 Answers1

0

I'm not sure I understand (or can reproduce) your issue.

Here is an example using the iris data that works just fine.

iris %>%
    mutate(x = rowSums(select(., contains("Width")))) %>%
    head()
#  Sepal.Length Sepal.Width Petal.Length Petal.Width Species   x
#1          5.1         3.5          1.4         0.2  setosa 3.7
#2          4.9         3.0          1.4         0.2  setosa 3.2
#3          4.7         3.2          1.3         0.2  setosa 3.4
#4          4.6         3.1          1.5         0.2  setosa 3.3
#5          5.0         3.6          1.4         0.2  setosa 3.8
#6          5.4         3.9          1.7         0.4  setosa 4.3

As you can see x is the sum of columns Sepal.Width and Petal.Width, and is the same as

rowSums(select(iris, contains("Width"))) %>% head()
#[1] 3.7 3.2 3.4 3.3 3.8 4.3
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • This only selects the first 6 entries not the entire set of observations for the selected columns. – psych.tek Jul 17 '18 at 05:19
  • 1
    @psych.tek That's because I *chose* to show only the first six entries with `head` for illustration purposes. Remove that line to get all, i.e. `iris %>% mutate(x = rowSums(select(., contains("Width"))))`. See basic `?head` for details. – Maurits Evers Jul 17 '18 at 05:28
  • ah ok. Bear with me, slowly getting my head around things. :) when i run: `rowSums(select(well_being_df2, contains("PWI")))` it works exactly as you have outlined. But when I run it inside mutate `mutate(well_being_df, x = rowSums(select(well_being_df2, contains("PWI"))))` it does not select the PWI but the entire set of rows. Thats where I am getting stuck. – psych.tek Jul 17 '18 at 05:36
  • Ok so it turns out my understanding of mutate is a little off. This does work but it was printing the entire set of columns **including** the newly created(mutated) one. – psych.tek Jul 17 '18 at 05:46
  • @psych.tek Yup, it adds a new column `x` with entries based on the row sum across columns starting `"PWI"`. You got it;-) – Maurits Evers Jul 17 '18 at 05:49