R and Python Cheat Sheet
Teaching · IE 3301 Engineering Probability
The same statistics in R and in Python, one line each. Rows marked Watch out are where the two differ in a way that gives a wrong answer without an error. Every row with a number (35 of them) was run in both languages and gives the same result. The worked examples Statistics Basics and Linear Regression and The Basics of Simulation use both languages step by step.
Getting started
| Task | R | Python |
|---|---|---|
| Tools R's statistics are built in; Python's come from three libraries. | | |
| Read a whitespace-separated file | | |
| Column, rows, size | | |
| Random seed Watch out: Same seed, different numbers: the two languages use different generators. | | |
Describing data
| Task | R | Python |
|---|---|---|
| Mean | | |
| Sample standard deviation Watch out: numpy's default (ddof=0) divides by n. pandas' .std() divides by n − 1, like R. | | |
| Sample variance | | |
| Percentile Both default to the same (linear) rule. | | |
| Summary | | |
| Covariance np.cov divides by n − 1 by default, unlike np.std. | | |
| Correlation | | |
| Histogram, scatter | | |
Distributions
R names each function by a letter and the distribution: d (density or mass), p (cumulative probability), q (quantile) and r (random draws). SciPy uses methods on the distribution: pdf or pmf, cdf, ppf and rvs.
| Task | R | Python |
|---|---|---|
| Normal density | | |
| P(Z ≤ 1.96) | | |
| P(Z > 1.96) | | |
| z for 97.5% | | |
| Five normal draws | | |
| Binomial P(X = 3) Watch out: Discrete distributions have pmf, not pdf, in SciPy. | | |
| Binomial P(X ≤ 3) | | |
| Poisson P(X = 2), mean 3 | | |
| Exponential P(X ≤ 2), rate 0.5 Watch out: SciPy takes the mean (scale = 1/rate), not the rate. | | |
| Gamma P(X ≤ 3), shape 2, rate 0.5 Watch out: Shape is a; again scale = 1/rate. | | |
| Uniform on [0, 2]: P(X ≤ 0.3) Watch out: SciPy's scale is the width, not the maximum. | | |
| t for 97.5%, 9 df | | |
| Chi-square 95th percentile, 4 df | | |
| F 95th percentile, 3 and 20 df | | |
Intervals and tests
| Task | R | Python |
|---|---|---|
| One-sample t test | | |
| 95% CI for the mean R prints it with every t test. | | |
| Two-sample t test Watch out: R defaults to Welch's test; SciPy pools the variances unless equal_var=False. | | |
| Paired t test | | |
| F test of two variances No one-line SciPy version. | | |
| Test of one proportion Watch out: From statsmodels; R matches it only with correct = FALSE. | | |
| Chi-square test of independence | | |
| Shapiro-Wilk normality test | | |
Regression
| Task | R | Python |
|---|---|---|
| Fit y on x The same formula language in both. | | |
| Coefficients | | |
| Full table | | |
| R² | | |
| CIs for the coefficients | | |
| Confidence interval for the mean at x = 5 Columns mean_ci_lower, mean_ci_upper. | | |
| Prediction interval at x = 5 Columns obs_ci_lower, obs_ci_upper. | | |
| Residual plots R draws four diagnostic plots at once. | | |
| Variance inflation factors Watch out: car in R. Python needs the design matrix, intercept included; skip column 0. | | |
| Breusch-Pagan test From lmtest in R. Both default to the studentized (Koenker) form. | | |
| One-way ANOVA Wrap a text column in C() in Python to treat it as categories. | | |
Simulation
| Task | R | Python |
|---|---|---|
| Roll a die ten times Watch out: Python's upper bound is excluded: 7, not 6. | | |
| 1,000 means of 5 exponentials Python draws a whole table at once and averages each row. | | |
Python here means numpy, pandas, SciPy and statsmodels (pip install numpy pandas scipy statsmodels matplotlib);
R needs only install.packages(c("car", "lmtest")) beyond what it ships with. Free in the browser:
Google Colab for Python and Posit Cloud for R.