Using R's summary(), I want to make a table that has means, std, n, min, and max for multiple variables. I will use mtcars as a dataset (R's default dataset). If just one variable, this worked well:
as.data.frame(t(unclass(summary(mtcars$disp))))
The result:
Min. 1st Qu. Median Mean 3rd Qu. Max.
1 71.1 120.825 196.3 230.7219 326 472
If more than one, it doesn't work well. I'm getting the same result as above (only the result for mtcars$disp shows).
as.data.frame(t(unclass(summary(mtcars$disp,mtcars$hp,mtcars$drat))))
The result (the same as above): Min. 1st Qu. Median Mean 3rd Qu. Max. 1 71.1 120.825 196.3 230.7219 326 472
The ideal result should look like this.
Min. 1st Qu. Median Mean 3rd Qu. Max.
71.1 120.825 196.3 230.7219 326 472
52 96.5 123 146.6875 180 335
2.76 3.08 3.695 3.596563 3.92 4.93
I would like the name of variables too:
Name Min. 1st Qu. Median Mean 3rd Qu. Max.
disp 71.1 120.825 196.3 230.7219 326 472
hp 52 96.5 123 146.6875 180 335
drat 2.76 3.08 3.695 3.596563 3.92 4.93
Could you advise? Also in the last code, I have to repeat $mtcars many times. Is there a way to avoid this?
Thank you.
I ask a similar question here, but the suggested codes are getting very complicated. I'd like to stick with summary() if possible. R question: how to save summary results into a dataset