tabstat
One compact table of exactly the statistics you ask for.
What we have
- statistics:
count,max,mean,median,min,n,p50,sd,sum by()for one table per groupaw,pwandfwweights
Coming later
- a group with fewer than
set mincellobservations loses its whole row
How you write it
tabstat varlist [weight], statistics(mean sd n sum median min max) [by(var)]
Square brackets mean optional. A vertical bar means choose one.
What it does
summarize gives you a fixed set. tabstat lets you choose: statistics(mean sd n) gives those three and nothing else, and by(sector) splits the table by group.
This is the command for a table you are going to paste into a report, because you control what is in it.
Try it
Every example below was run after sysuse vira.sample02, clear.
. tabstat mpce, statistics(mean sd n) by(sector)
mpce
mean std count
sector
1 3401.220364 1311.434416 3987
2 4347.226016 1602.912269 2013
The same thing in pandas and in R
pandas
df.groupby(['sector'])[['mpce']].agg(['mean', 'std', 'count'])
R
library(dplyr)
df %>% group_by(sector) %>% summarise(mpce_mean = mean(mpce,
na.rm = TRUE), mpce_sd = sd(mpce, na.rm = TRUE),
mpce_n = sum(!is.na(mpce))) %>% ungroup()
# This R was run in a real R (4.6.1) and gave the same numbers.