summarize
The standard summary of a number column.
What we have
- observations, mean, standard deviation, minimum and maximum
detailfor percentiles, skewness and kurtosisawandfwweightsr(N),r(mean),r(sd),r(min),r(max)andr(Var)for the last variable
Coming later
- the percentiles are printed by
detailbut not left inr()— there is nor(p50) - while
set mincellis on, Min and Max are always hidden
You may shorten it to su.
How you write it
summarize [varlist] [if] [in] [weight] [, detail]
Square brackets mean optional. A vertical bar means choose one.
What it does
How many values there are, the typical value (mean), how spread out they are (standard deviation), and the smallest and largest.
Add detail and you get considerably more: the middle value (median), the values that cut the data into quarters and hundredths (percentiles), how lopsided the distribution is (skewness) and how heavy its tails are (kurtosis), plus the four smallest and four largest values.
The last variable's results are left in r() — r(mean), r(sd), r(N) — for a later command to pick up.
Try it
Every example below was run after sysuse vira.sample02, clear.
. summarize mpce age
Variable | Obs Mean Std. dev. Min Max
-------------+------------------------------------------------------------
mpce | 6,000 3718.605 1484.595 647.49 12520.92
age | 6,000 47.0195 18.76303 15 79
The same thing in pandas and in R
pandas
df[['mpce', 'age']].describe()
R
library(dplyr)
df %>% summarise(mpce_n = sum(!is.na(mpce)), mpce_mean = mean(mpce,
na.rm = TRUE), mpce_sd = sd(mpce, na.rm = TRUE),
mpce_min = min(mpce, na.rm = TRUE), mpce_max = max(mpce,
na.rm = TRUE), age_n = sum(!is.na(age)), age_mean = mean(age,
na.rm = TRUE), age_sd = sd(age, na.rm = TRUE), age_min = min(age,
na.rm = TRUE), age_max = max(age, na.rm = TRUE))
# This R was run in a real R (4.6.1) and gave the same numbers.
. summarize mpce, detail
mpce
--------------------------------------------------------------
Percentiles Smallest
1% 1387.961 647.49
5% 1788.047 912.75
10% 2083.438 956.43
25% 2675.188 1051.15
50% 3457.63 Largest
75% 4508.285 11309.04
90% 5701.137 11740.99
95% 6559.135 11937.06
99% 8375.111 12520.92
Obs 6,000
Sum of wgt. 6,000
Mean 3718.605
Std. dev. 1484.595
Variance 2204024
Skewness 1.114374
Kurtosis 4.898832
The same thing in pandas and in R
pandas
from scipy import stats values = df["mpce"].dropna().sort_values() values.quantile([.01, .05, .10, .25, .50, .75, .90, .95, .99]) values.agg(["count", "mean", "std", "var"]) stats.skew(values), stats.kurtosis(values, fisher=False) values.head(4), values.tail(4)
R
library(dplyr)
df %>% summarise(n = sum(!is.na(mpce)), mean = mean(mpce,
na.rm = TRUE), sd = sd(mpce, na.rm = TRUE))
quantile(df$mpce, c(.01,.05,.10,.25,.50,.75,.90,.95,.99), na.rm = TRUE)
# This R was run in a real R (4.6.1) and gave the same numbers.