0
votes

I cant seem to find how to make a simple table with the mean of one variable over other categorical variables. So I want the mean of var1 (ratio 0-100) over var2 and var3 (both dummy with value 1 and value 2). in a simple table below each other. so var 1 in the column and var2 and var3 in the rows. I thought maybe the compareGroups package could help but I cannot figure out how.

1

1 Answers

0
votes

Using base R with the mtcars data set:

aggregate(mpg ~ am, data = mtcars,mean)

...and the output:

> aggregate(mpg ~ am, data = mtcars,mean)
  am      mpg
1  0 17.14737
2  1 24.39231
> 

A dplyr solution looks like:

library(dplyr)
mtcars %>% group_by(am) %>% summarise(mean = mean(mpg))
> mtcars %>% group_by(am) %>% summarise(mean = mean(mpg))
# A tibble: 2 x 2
     am  mean
  <dbl> <dbl>
1     0  17.1
2     1  24.4
>