Help averaging multiple scores into new column

Hi community,

This is my very first time using R to analyze my research data. It's a psychology study, and we used 10 different scales to measure our variables.
I finally organized the data frame sufficiently to analyze it, but now I realize I can't quite find how to average my scores.

For example, I have 80 rows (participants), and one scale is 20 items.
All the odd columns are variable A and all the even columns are variable B.
I need to have an average variable score for each participant, is there a way to do that?

Any help is appreciated, I'm feeling pretty lost

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

Hi, I made a sample dataset,
Here's an example dataset

data.frame(
       Item1 = c(3, 4, 4, 6, 7),
       Item2 = c(6, 7, 7, 8, 5),
       Item3 = c(5, 4, 3, 5, 7),
       Item4 = c(3, 8, 9, 8, 7),
       Item5 = c(4, 5, 6, 3, 2),
       Item6 = c(7, 8, 6, 5, 8)
)

In this set, items 1, 3, and 5 would be subscale 1 and items 2, 4, and 6 would be subscale 2.
I need to find the averages for both subscales, and preferably make them into two separate columns.
I do not have a code error - because I just can't find a code to use.
I tried averaging like this:

 > mean(mydata$Item1$mydata$Item3$mydata$Item5)
Error in mydata$Item1$ : $ operator is invalid for atomic vectors

But that definitely doesn't work.

Is there anything else I can do to help or mak this clearer?

Hi, I recommend using the tidyverse library (this would require installing it, if you havent yet)
Also I show two ways to calculate the mean , one is more 'manual' than the other, but the more automatic one, requires the rowwise() function whereas the other would work fine without it.

start_df <- data.frame(
  Item1 = c(3, 4, 4, 6, 7),
  Item2 = c(6, 7, 7, 8, 5),
  Item3 = c(5, 4, 3, 5, 7),
  Item4 = c(3, 8, 9, 8, 7),
  Item5 = c(4, 5, 6, 3, 2),
  Item6 = c(7, 8, 6, 5, 8)
)

library(tidyverse)
average_scale_scores_per_particpant <- mutate(rowwise(start_df),
                  sub_scale_1_avg= mean(c(Item1,Item3,Item5)),
                  sub_scale_1_avg_alt= (Item1+Item3 + Item5) /3 , # this one doesnt need the rowwise but the others do
                  sub_scale_2_avg = mean(c(Item2,Item4,Item6))
) %>% ungroup

average_scale_scores_per_particpant
# A tibble: 5 x 9
# Item1 Item2 Item3 Item4 Item5 Item6 sub_scale_1_avg sub_scale_1_avg_alt sub_scale_2_avg
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>           <dbl>               <dbl>           <dbl>
# 1     3     6     5     3     4     7            4                   4               5.33
# 2     4     7     4     8     5     8            4.33                4.33            7.67
# 3     4     7     3     9     6     6            4.33                4.33            7.33
# 4     6     8     5     8     3     5            4.67                4.67            7   
# 5     7     5     7     7     2     8            5.33                5.33            6.67
2 Likes

Thank you so much!
It worked! I used the automatic way because with this df it would be possible to enter all of values, whereas in my actual df theres well over 20 items per scale as over 80 participants.

It gave me this tibble:

> avg_score_pp
# A tibble: 10 x 12
   Participants Item1 Item2 Item3 Item4 Item5 Item6 Item7 Item8 sub_scale_1_avg sub_scale_1_avg…
          <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>           <dbl>            <dbl>
 1            1     3     6     5     3     4     7     6     5            4                4   
 2            2     4     7     4     8     5     8     2     7            4.33             4.33
 3            3     4     7     3     9     6     6     3     6            4.33             4.33
 4            4     6     8     5     8     3     5     4     4            4.67             4.67
 5            5     7     5     7     7     2     8     2     8            5.33             5.33
 6            6     2     4     5     7     2     7     4     9            3                3   
 7            7     9     6     8     8     1     5     3     9            6                6   
 8            8     1     8     5     6     5     7    67     4            3.67             3.67
 9            9     2     7     3     5     6     3     5     3            3.67             3.67
10           10     4     3     5     4     4     8     4     7            4.33             4.33
# … with 1 more variable: sub_scale_2_avg <dbl>

How can I see the hidden variable?

Also, I have some scales that have reversed items - is there a way to reverse those before calculating the mean?

Thank you again, and sorry for all the questions, I'm very new to all of this.

Hello again Nir,

I tried applying this to my actual dataset today and got this error:

> library(tidyverse)
> 
> average_VIA_scores <- mutate(rowwise(thesis),
+                                               
+                                               VIA_HC_avg= mean(c(VIA_1,VIA_3,VIA_5,VIA_7,VIA_9,VIA_11,VIA_13,VIA_15,VIA_17,VIA_19)),
+                                               
+                                               sub_scale_1_avg_alt= (VIA_1,VIA_3,VIA_5,VIA_7,VIA_9,VIA_11,VIA_13,VIA_15,VIA_17,VIA_19) /3 , # this one doesnt need the rowwise but the others do
Error: unexpected ',' in:
"                                              
                                              sub_scale_1_avg_alt= (VIA_1,"
>                                               
>                                               VIA_MC_avg = mean(c(VIA_2,VIA_4,VIA_6,VIA_8,VIA_10,VIA_12,VIA_14,VIA_16,VIA_18,VIA_20))
Error in mean(c(VIA_2, VIA_4, VIA_6, VIA_8, VIA_10, VIA_12, VIA_14, VIA_16,  : 
  object 'VIA_2' not found
>                                               
> ) %>% ungroup
Error: unexpected ')' in ")"
> 
> 
> 
> average_VIA_scores
Error: object 'average_VIA_scores' not found

I was wondering if you could maybe help me understand what went wrong?

Check that you are using c function when you list multiple things as a vector