I am trying to create a data table that sorts each of the variable rows into 2 different subgroups. Here is a reproducible example to hopefully clarify what I'd like to do.
table <- mtcars %>%
group_by(vs) %>%
summarise(MPG=mean(mpg),DRAT=mean(drat))
table=t(table)
table
[,1] [,2]
vs 0.000000 1.000000
MPG 16.616667 24.557143
DRAT 3.392222 3.859286
#here is what I would like the output to look like, but am unsure how to do create it (some of these numbers I just made up)
**[,1] [,2]
vs 0.000000 1.000000
MPG (overall) 16.616667 24.557143**
MPG (am=1) 14 13
MPG (am=0) 11 12
**DRAT (overall) 3.392222 3.859286**
DRAT (am=1) 3.1 3.6
DRAT (am=0) 4 4
So with that table in mind, I want to modify it so for the MPG and DRAT variables, there are two subcategories based on whether cars are in the 0 or 1 group for the am variable in the data set. I'm having a hard time telling R to have column groups for the vs variable, and also separately analyzing the am groups (0 or 1) for each row and displaying that in the table. Thank you!
You could use the tables
package :
tabular((mpg+drat)*(am=factor(am)+1)~(vs=factor(vs)*(mean)), data=mtcars)
vs
0 1
am mean mean
mpg 0 15.050 20.743
1 19.750 28.371
All 16.617 24.557
drat 0 3.121 3.570
1 3.935 4.149
All 3.392 3.859