Explore data

Start with the expanded example for each command. Open the variants for options and alternative R approaches. Read the qualifications before adapting the code.

anova

Analysis of variance

Stata

anova y group

R Package:stats

summary(aov(y ~ factor(group), data = data))

browse

View the data in a spreadsheet window

Stata

browse

R Package:tidystata

browse(data)
9 more examples and alternatives

View selected variables

Stata

browse x1 x3-x5

R Package:tidystata

browse(data, x1, x3:x5)

Ranges of variables use a colon (x3:x5), not a dash.

Variables whose names end with given text

Stata

browse *x

R Package:dplyr

select(data, ends_with("x"))

Variables whose names contain given text

Stata

browse *x*

R Package:dplyr

select(data, contains("x"))

Rows meeting a condition

Stata

browse if x1 == 1 & x2 == 2

R Package:dplyr

filter(data, x1 == 1 & x2 == 2)

The last rows

Stata

browse in -10/-1

R Package:base

tail(data, 10)

The first rows

Stata

browse in 1/10

R Package:base

head(data, 10)

Rows by position

Stata

browse in 7/12

R Package:dplyr

slice(data, 7:12)

Variables whose names start with given text

Stata

browse x*

R Package:dplyr

select(data, starts_with("x"))

View variables matching Stata wildcards

Stata

browse x* y*

R Package:tidystata

browse(data, varlist("x* y*"))

varlist() understands Stata’s * and ? wildcards and works in any selection, such as select().

ci

Confidence interval of a mean

Stata

ci mean x

R Package:stats

t.test(data$x)$conf.int

Add conf.level = 0.99 for ci mean x, level(99).

codebook

Describe every variable

Stata

codebook

R Package:tidystata

codebook(data)
1 more examples and alternatives

One line of statistics per variable

Stata

codebook, compact

R Package:tidystata

summ(data)

correlate

Correlation matrix (complete cases)

Stata

correlate x1 x2 x3

R Package:stats

cor(select(data, x1, x2, x3), use = "complete.obs")

describe

List variables with their types and labels

Stata

describe

R Package:tidystata

variables(data)

glimpse(data) gives a compact view with the first values of each variable.

1 more examples and alternatives

Variables in a file, without loading its data

Stata

describe using data.dta

R Package:haven

haven::read_dta("data.dta", n_max = 0)

ds

List variable names

Stata

ds

R Package:tidystata

ds(data)

ds(data, .type = “table”) returns the names as a one-column table; names(data) is base R.

dtable

Descriptive Table 1 (Stata 18+)

Stata

dtable x1 x2, by(group)

R Package:gtsummary

gtsummary::tbl_summary(data, by = group, include = c(x1, x2))

modelsummary::datasummary_skim(data) gives a quick overview without a by variable.

factor

Factor analysis

Stata

factor x1 x2 x3

R Package:stats

factanal(na.omit(select(data, x1, x2, x3)), factors = 1)

Differs from Stata factanal() uses maximum likelihood; psych::fa(…, fm = “pa”) gives Stata’s default principal-factor method.

fre

Frequency table with totals

Stata

fre x

R Package:tidystata

fre(data, x)

Like Ben Jann’s fre: tab() with a total row.

inspect

Distribution, zeros and missing values of a variable

Stata

inspect x

R Package:tidystata

summ(data, x, .detail = TRUE)

hist(data$x) adds the quick histogram that inspect draws.

ksmirnov

Kolmogorov-Smirnov test of equal distributions

Stata

ksmirnov x, by(group)

R Package:stats

ks.test(data$x[data$group == 0], data$x[data$group == 1])

Differs from Stata The R code assumes group codes 0 and 1; exact/asymptotic inference and tied-value handling can differ from Stata ksmirnov.

ktau

Kendall’s tau

Stata

ktau x y

R Package:stats

cor.test(data$x, data$y, method = "kendall")

Differs from Stata The rank coefficient has the same interpretation, but exact/asymptotic p-value selection and tie corrections may differ; match inference options separately.

kwallis

Kruskal-Wallis test

Stata

kwallis x, by(group)

R Package:stats

kruskal.test(x ~ group, data = data)

levelsof

Distinct values of a variable

Stata

levelsof x

R Package:base

sort(unique(data$x))

Differs from Stata Store them for a loop: levels <- sort(unique(data$x)), then for (v in levels) { … }. sort(unique()) excludes NA by default, but string ordering follows R collation and can differ from Stata sorting.

list

Print the first and last rows

Stata

list

R Package:tidystata

print_all(data)

Prints every row and column. Typing data prints the first rows; print_headtail(data) prints the first and last rows.

1 more examples and alternatives

Print one variable

Stata

list x

R Package:dplyr

pull(data, x)

data$x does the same. select(data, x) keeps x as a one-column table.

lookfor

Find variables by name or label

Stata

lookfor income

R Package:tidystata

variables_search(data, "income")

mean

Mean with its standard error

Stata

mean x

R Package:tidystata

summ(data, x, .stat = c("mean", "se"))

Differs from Stata t.test(data$x) also reports the confidence interval. The R snippet returns point summaries only (plus SE for mean); Stata also reports confidence intervals and estimation results.

oneway

One-way analysis of variance

Stata

oneway y group

R Package:stats

summary(aov(y ~ factor(group), data = data))

Differs from Stata bartlett.test(y ~ group, data = data) gives the equal-variance test that oneway reports. aov() gives the ANOVA table but does not include the Bartlett equal-variance test printed by oneway; request bartlett.test() separately.

pca

Principal components

Stata

pca x1 x2 x3

R Package:stats

prcomp(na.omit(select(data, x1, x2, x3)), scale. = TRUE)

Differs from Stata Stata analyses the correlation matrix, so scale. = TRUE matches it. scale. = TRUE matches correlation PCA; signs of eigenvectors are arbitrary and the stored rotations and printed loadings have different normalization conventions.

proportion

Proportion in each category

Stata

proportion x

R Package:base

prop.table(table(data$x))

Differs from Stata tab(data, x) shows the same percentages; prop.test() adds a confidence interval for one proportion. The R snippet returns point summaries only (plus SE for mean); Stata also reports confidence intervals and estimation results.

prtest

Test of a proportion

Stata

prtest x == 0.5

R Package:stats

prop.test(sum(data$x == 1, na.rm = TRUE), sum(!is.na(data$x)), p = 0.5, correct = FALSE)

Differs from Stata correct = FALSE gives Stata’s normal-approximation test without continuity correction. The uncorrected test statistic matches the normal approximation, but prop.test() confidence intervals use score inversion rather than the prtest Wald interval.

pwcorr

Pairwise correlations

Stata

pwcorr x1 x2 x3

R Package:tidystata

pwcorr(data, x1, x2, x3)

Differs from Stata Gives r, n and p for each pair; .wide = TRUE gives the matrix. Base R: cor(select(data, x1, x2, x3), use = “pairwise.complete.obs”). pwcorr() returns pair counts and p-values in a tidy table; Stata prints a correlation matrix by default and needs options for additional reporting.

ranksum

Wilcoxon rank-sum (Mann-Whitney) test

Stata

ranksum x, by(group)

R Package:stats

wilcox.test(x ~ group, data = data)

Differs from Stata R can choose exact inference in small samples and applies continuity correction in its normal approximation; Stata defaults differ. Set exact and correct deliberately.

signrank

Wilcoxon signed-rank test

Stata

signrank x = y

R Package:stats

wilcox.test(data$x, data$y, paired = TRUE)

Differs from Stata R can choose exact inference in small samples and applies continuity correction in its normal approximation; Stata defaults differ. Set exact and correct deliberately.

sktest

Skewness and kurtosis

Stata

sktest x

R Package:tidystata

summ(data, x, .stat = c("skew", "kurtosis"))

For a formal normality test use shapiro.test(data$x).

spearman

Spearman rank correlation

Stata

spearman x y

R Package:stats

cor.test(data$x, data$y, method = "spearman")

Differs from Stata The rank coefficient has the same interpretation, but exact/asymptotic p-value selection and tie corrections may differ; match inference options separately.

stem

Stem-and-leaf display

Stata

stem x

R Package:graphics

stem(data$x)

summarize

Summary statistics for all variables

Stata

summarize

R Package:tidystata

summ(data)

Differs from Stata summary(data) is the base R version, with a less tidy layout. summ() also reports missing counts and has its own default statistic layout; select .stat explicitly when matching Stata output.

4 more examples and alternatives

Summary statistics for one variable

Stata

summarize x

R Package:tidystata

summ(data, x)

Differs from Stata summ() also reports missing counts and has its own default statistic layout; select .stat explicitly when matching Stata output.

Summary statistics by group

Stata

bysort group: summarize x

R Package:tidystata

summ(data, x, .by = group)

Differs from Stata group_by(data, group) |> summ(x) gives the same result. summ() also reports missing counts and has its own default statistic layout; select .stat explicitly when matching Stata output.

Weighted summary statistics

Stata

summarize x [aweight = w]

R Package:tidystata

summ(data, x, .wt = w, .wt_type = "analytic")

Differs from Stata Frequency weights are the default: summ(data, x, .wt = w). summ() also reports missing counts and has its own default statistic layout; select .stat explicitly when matching Stata output.

Detailed statistics, including percentiles

Stata

summarize x, detail

R Package:tidystata

summ(data, x, .detail = TRUE)

Differs from Stata Pick single statistics with .stat, as in summ(data, x, .stat = “p25”). summ() also reports missing counts and has its own default statistic layout; select .stat explicitly when matching Stata output.

swilk

Shapiro-Wilk normality test

Stata

swilk x

R Package:stats

shapiro.test(data$x)

Differs from Stata shapiro.test() accepts 3 to 5000 nonmissing observations; Stata swilk has different sample-size limits and reporting.

tab1

One frequency table per variable

Stata

tab1 x1 x2 x3

R Package:tidystata

tab1(data, x1, x2, x3)

Differs from Stata tidystata retains missing categories by default; Stata tabulate excludes them unless missing is specified. Use .missing = FALSE to match Stata.

tab2

Cross-tabulation of two variables

Stata

tab2 x1 x2

R Package:tidystata

tab2(data, x1, x2)

Differs from Stata With three or more variables, tab2(data, x1, x2, x3) returns every pairwise table, as Stata does. tidystata retains missing categories by default; Stata tabulate excludes them unless missing is specified. Use .missing = FALSE to match Stata.

table

Joint frequencies of several variables

Stata

table x1 x2 x3

R Package:tidystata

tab(data, x1, x2, x3)
1 more examples and alternatives

A statistic by category (Stata 17+ table)

Stata

table x, statistic(mean y)

R Package:tidystata

summ(data, y, .by = x, .stat = "mean")

tabstat

Chosen statistics by group

Stata

tabstat x1 x2, by(group) stat(mean sd)

R Package:tidystata

tabstat(data, x1, x2, .by = group, .stat = c("mean", "sd"))

Differs from Stata R retains missing grouping keys; Stata tabstat by() and tabulate summarize() exclude missing grouping categories unless requested. Check the intended grouping sample.

tabulate

One-way frequency table

Stata

tabulate x

R Package:tidystata

tab(data, x)

Differs from Stata tidystata retains missing categories by default; Stata tabulate excludes them unless missing is specified. Use .missing = FALSE to match Stata.

8 more examples and alternatives

Two-way cross-tabulation

Stata

tabulate x1 x2

R Package:tidystata

tab2(data, x1, x2)

Differs from Stata fre2(data, x1, x2) adds row and column totals. tidystata retains missing categories by default; Stata tabulate excludes them unless missing is specified. Use .missing = FALSE to match Stata.

Frequency table by group

Stata

bysort g: tabulate x

R Package:tidystata

tab(data, x, .by = g)

Differs from Stata tidystata retains missing categories by default; Stata tabulate excludes them unless missing is specified. Use .missing = FALSE to match Stata.

Frequencies with analytic (sampling) weights

Stata

tabulate x [aweight = w]

R Package:tidystata

tab(data, x, .wt = w, .wt_type = "analytic")

Differs from Stata tidystata retains missing categories by default; Stata tabulate excludes them unless missing is specified. Use .missing = FALSE to match Stata.

Frequencies with frequency weights

Stata

tabulate x [fweight = n]

R Package:tidystata

tab(data, x, .wt = n)

Differs from Stata tidystata retains missing categories by default; Stata tabulate excludes them unless missing is specified. Use .missing = FALSE to match Stata.

Frequencies including missing values

Stata

tabulate x, missing

R Package:tidystata

tab(data, x)

tab() always lists missing values as a separate row.

Summary of y within each category of x

Stata

tabulate x, summarize(y)

R Package:tidystata

summ(data, y, .by = x)

Differs from Stata R retains missing grouping keys; Stata tabstat by() and tabulate summarize() exclude missing grouping categories unless requested. Check the intended grouping sample.

Chi-square test of independence

Stata

tabulate x1 x2, chi2

R Package:tidystata

tab2(dplyr::filter(data, !is.na(x1), !is.na(x2)), x1, x2, .chi2 = TRUE)

Differs from Stata Filter missing values on both variables for plain Stata tabulate, chi2. tab2 otherwise includes its displayed missing categories in the Pearson test. tidystata retains missing categories by default; Stata tabulate excludes them unless missing is specified. Use .missing = FALSE to match Stata.

Fisher’s exact test

Stata

tabulate x1 x2, exact

R Package:stats

fisher.test(data$x1, data$x2)

ttest

Two-sample t test

Stata

ttest x, by(group)

R Package:stats

t.test(x ~ group, data = data)

Differs from Stata R’s default is Welch’s test; add var.equal = TRUE for Stata’s default equal-variance test.

univar

Detailed univariate statistics

Stata

univar x1 x2

R Package:tidystata

summ(data, x1, x2, .detail = TRUE)