0

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.

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
es_dutch
  • 111
  • 1
  • 8
  • Please [edit as shown here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – NelsonGon Apr 30 '20 at 11:41

1 Answers1

0

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
> 
Len Greski
  • 10,505
  • 2
  • 22
  • 33
  • Thank you, this totally works. I am new to R so this is really helpful! – es_dutch Apr 30 '20 at 12:06
  • @es_dutch - If the answer was helpful, please accept and upvote it. Thanks! – Len Greski Apr 30 '20 at 12:08
  • Is there also a possiblity to perform a t-test to compare the means and see if they are significantly different? – es_dutch Apr 30 '20 at 12:32
  • @es_dutch - Yes there is: `t.test(mpg ~ am,data = mtcars)` . BTW, there are a number of resources to help you learn R, such as those I list in [References for R Programming](https://github.com/lgreski/datasciencectacontent/blob/master/markdown/rprog-References.md) – Len Greski Apr 30 '20 at 12:36
  • Thank you! It is a lot to figure out. – es_dutch Apr 30 '20 at 13:58