0

I'm trying to figure out a tidyverse way to create a composite measure from existing columns. I don't understand why I'm getting an integer when trying to calculate an average using the mean() function.

I've read that using rowwise() is discouraged so I tried a solution using group_by().

library(tidyverse)
tstdata <- tibble(id=1:30
                  ,fake1 = sample(c(1:7), replace = TRUE, size=30)
                  ,fake2 = sample(c(1:7), replace = TRUE, size=30)
                  ,fake3 = sample(c(1:7), replace = TRUE, size=30))
tstdata %>% mutate(fakeadd = fake1 + fake2 + fake3) -> tstdata
tstdata %>% group_by(id) %>% mutate(fakesum = sum(fake1,fake2,fake3)) %>% ungroup() -> tstdata
tstdata %>% mutate(fakeavg = (fake1+fake2+fake3)/3) -> tstdata
tstdata %>% group_by(id) %>% mutate(fakemean = mean(fake1,fake2,fake3)) %>% ungroup() -> tstdata

str(tstdata)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   30 obs. of  8 variables:
 $ id      : int  1 2 3 4 5 6 7 8 9 10 ...
 $ fake1   : int  6 5 6 7 6 6 5 3 4 3 ...
 $ fake2   : int  7 5 4 6 7 7 5 6 6 5 ...
 $ fake3   : int  1 2 2 1 3 7 2 1 4 6 ...
 $ fakeadd : int  14 12 12 14 16 20 12 10 14 14 ...
 $ fakesum : int  14 12 12 14 16 20 12 10 14 14 ...
 $ fakeavg : num  4.67 4 4 4.67 5.33 ...
 $ fakemean: int  6 5 6 7 6 6 5 3 4 3 ...

The sum() function used with group_by() gives the same result as my own formula. I'm confused by the results using the mean() function. I get integer values in that column that don't even seem to be rounded properly in some cases. I'd like to be able to handle missing data using na.rm. What am I missing? I have more experience with SPSS and I'm new to Tidyverse concepts.

I added a couple lines based on suggestions in comments:

library(tidyverse)
tstdata <- tibble(id=1:30
                  ,fake1 = sample(c(1:7), replace = TRUE, size=30)
                  ,fake2 = sample(c(1:7), replace = TRUE, size=30)
                  ,fake3 = sample(c(1:7), replace = TRUE, size=30))
tstdata %>% mutate(fakeadd = fake1 + fake2 + fake3) -> tstdata
tstdata %>% group_by(id) %>% mutate(fakesum = sum(fake1,fake2,fake3)) %>% ungroup() -> tstdata
tstdata %>% mutate(fakeavg = (fake1+fake2+fake3)/3) -> tstdata
tstdata %>% group_by(id) %>% mutate(fakemean = mean(fake1,fake2,fake3)) %>% ungroup() -> tstdata
tstdata %>% mutate(fakerowmean = rowMeans(.[c(fake1,fake2,fake3)])) -> tstdata
tstdata %>% group_by(id) %>% mutate(fakemean3 = mean(c(fake1,fake2,fake3))) %>% ungroup() -> tstdata
str(tstdata)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   30 obs. of  10 variables:
 $ id         : int  1 2 3 4 5 6 7 8 9 10 ...
 $ fake1      : int  5 6 1 3 3 3 7 7 1 4 ...
 $ fake2      : int  5 1 6 6 3 6 1 6 7 5 ...
 $ fake3      : int  6 4 1 6 2 1 6 4 5 6 ...
 $ fakeadd    : int  16 11 8 15 8 10 14 17 13 15 ...
 $ fakesum    : int  16 11 8 15 8 10 14 17 13 15 ...
 $ fakeavg    : num  5.33 3.67 2.67 5 2.67 ...
 $ fakemean   : int  5 6 1 3 3 3 7 7 1 4 ...
 $ fakerowmean: num  8.02 5.72 4.57 8.17 4.91 ...
 $ fakemean3  : num  5.33 3.67 2.67 5 2.67 ...

Changing the arguments in the mean() function gives matching results now. I tried using rowMeans() the way it was formatted in the comments, but I don't know where those are coming from. They are not means of the 3 columns. Thank you for the quick comments!

CiM
  • 1
  • 2
  • Looks like you need `rowMeans(.[c('fake1', 'fake2', 'fake3')])` – akrun Oct 14 '19 at 16:44
  • 1
    The `mean()` function works across a vector, not the parameters you pass to it. Look the the difference in the results between `mean(1,2,3)` and `mean(c(1,2,3))` – MrFlick Oct 14 '19 at 16:46
  • Potential duplicate: https://stackoverflow.com/questions/10945703/calculate-row-means-on-subset-of-columns – GenesRus Oct 14 '19 at 23:20

1 Answers1

0

I really appreciate the suggestions. I got the rowMeans() and the mean() functions to work. Here's the working example.

library(tidyverse)
tstdata <- tibble(id=1:30
                  ,fake1 = sample(c(1:7), replace = TRUE, size=30)
                  ,fake2 = sample(c(1:7), replace = TRUE, size=30)
                  ,fake3 = sample(c(1:7), replace = TRUE, size=30))
tstdata %>% mutate(fakeadd = fake1 + fake2 + fake3) -> tstdata
tstdata %>% group_by(id) %>% mutate(fakesum = sum(c(fake1,fake2,fake3))) %>% ungroup() -> tstdata
tstdata %>% mutate(fakeavg = (fake1+fake2+fake3)/3) -> tstdata
tstdata %>% mutate(fakerowmean = rowMeans(.[c("fake1","fake2","fake3")])) -> tstdata
tstdata %>% group_by(id) %>% mutate(fakemean3 = mean(c(fake1,fake2,fake3))) %>% ungroup() -> tstdata
str(tstdata)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   30 obs. of  9 variables:
 $ id         : int  1 2 3 4 5 6 7 8 9 10 ...
 $ fake1      : int  5 2 2 6 6 6 1 7 2 6 ...
 $ fake2      : int  5 4 1 4 2 4 6 6 4 6 ...
 $ fake3      : int  6 7 2 5 1 3 7 1 5 6 ...
 $ fakeadd    : int  16 13 5 15 9 13 14 14 11 18 ...
 $ fakesum    : int  16 13 5 15 9 13 14 14 11 18 ...
 $ fakeavg    : num  5.33 4.33 1.67 5 3 ...
 $ fakerowmean: num  5.33 4.33 1.67 5 3 ...
 $ fakemean3  : num  5.33 4.33 1.67 5 3 ...
CiM
  • 1
  • 2