When comparing groups of individuals or observations, the focus is often on descriptive statistics for each group, rather than on the overall sample
Statistical information. Similarly, there are several ways to accomplish this task in R. We will get the gearbox type at all levels of
Descriptive statistics begin.
vars<-C ("MPG", "HP", "WT")
> Aggregate (Mtcars[vars],by=list (AM=MTCARS$AM), median)
AM mpg hp WT
1 0 17.3) 175 3.52
2 1 22.8) 109 2.32
Note the use of List (AM=MTCARS$AM). If you are using list (MTCARS$AM), the AM column will be labeled
GROUP.1 rather than AM. You use this assignment to specify a more helpful column label. If you have more than one grouping variable, you can
Use statements such as By=list (NAME1=GROUPVAR1, Name2=groupvar2, ..., Groupvarn).
> Aggregate (Mtcars[vars],by=list (am=mtcars$am,gear=mtcars$gear), median)
AM Gear mpg HP WT
1 0 3 15.50 180.0 3.730
2 0 4 21.00 109.0 3.315
3 1 4 25.05 79.5 2.260
4 1 5 19.70 175.0 2.770
------------------------------------------------
The Doby package and the psych package also provide a function for grouping computational descriptive statistics. Likewise, they are not released with the basic installation,
Must be installed prior to first use. The Summaryby () function in the Doby package is used in the format
> summaryby (Mpg+hp+wt~am,data=mtcars,fun=mtstat)
Am MPG.N mpg.mean mpg.stdev mpg.skew mpg.kurtosis hp.n hp.mean hp.stdev hp.skew hp.kurtosis wt.n wt.mean wt.stdev WT . Skew Wt.kurtosis
1 0 32 20.09062 6.026948 0.610655-0.372766 32 146.6875 68.56287 0.7260237-0.1355511 32 3.21725 0.9784574 0. 4231465-0.02271075
>
> describe.by (mtcars[vars],mtcars$am)
group:0
VARs n mean sd median trimmed mad min max range skew kurtosis se
MPG 1 32 20.09 6.03 19.20 19.70 5.41 10.40 33.90 23.50 0.61-0.37 1.07
HP 2 32 146.69 68.56 123.00 141.19 77.10 52.00 335.00 283.00 0.73-0.14 12.12
WT 3 32 3.22 0.98 3.33 3.15 0.77 1.51 5.42 3.91 0.42-0.02 0.17
------------------------------------------------------------------------------
Reshape package to derive descriptive statistics by group flexibly
> dfs <-Melt (mtcars,measure.vars=c ("mpg", "HP", "WT"), Id.vars=c ("AM", "cyl"))
> Cast (dfs,am+cyl+variable~.,dstats)
AM cyl variable n mean SD
1 0 4 mpg 11 26.663636 4.5098277
2 0 4 HP 11 82.636364 20.9345300
3 0 4 WT 11 2.285727 0.5695637
4 0 6 mpg 7 19.742857 1.4535670
5 0 6 hp 7 122.285714 24.2604911
6 0 6 wt 7 3.117143 0.3563455
7 0 8 mpg 14 15.100000 2.5600481
8 0 8 HP 14 209.214286 50.9768855
9 0 8 WT 14 3.999214 0.7594047
R Language Note Grouping computational descriptive statistics aggregate, summaryby, describe.by