Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

A one-page cheat sheet for the R this course actually uses: the two packages you load, the one formula pattern that runs through nearly every command, and the mosaic/BSDA functions that match each calculator procedure in the TI-83/84 Quick Guide. Every function here is taught step by step, with real output, in the R-Help lessons — this page is the short version to keep open while you work. Nothing here goes beyond what MATH 1209 covers (Ch 1–7): one proportion, one mean, and the ideas that build up to them. Remember: R is always optional in this course — everything below has a calculator equivalent.


11. Getting started, every session

library(mosaic)   # summaries, plots (ggformula), simulation
library(BSDA)     # z/t tests from summary statistics (zsum.test, tsum.test)

That is the entire package list for MATH 1209, all semester (see R-Help Lesson 5). On the CSUB JupyterHub (https://csub.jupyter.cal-icor.org/) both are pre-installed — you install nothing, ever.


22. Getting data into R

TaskCodeNotes
Load a dataset built into mosaic/mosaicDatadata(KidsFeet)see the dataset index
Peek at structurestr(KidsFeet) · glimpse(KidsFeet)variable names, types, first values
Quick summary of every columninspect(KidsFeet)numeric and categorical summaries at once
First / last rowshead(KidsFeet) · tail(KidsFeet)default 6 rows
Dimensionsnrow(KidsFeet) · ncol(KidsFeet)rows × columns
Column namesnames(KidsFeet)
Read your own CSV file (e.g., for a project)dat <- read.csv("myfile.csv")see R-Help Lesson 6 for CSV, Excel, and URL imports

33. The formula interface at a glance

You want…Formula shapeExample
one variable~ yfavstats(~ length, data = KidsFeet)
a variable by a groupy ~ xfavstats(length ~ sex, data = KidsFeet)
one categorical variable~ ytally(~ sex, data = KidsFeet)
a two-way table~ y + xtally(~ sex + biggerfoot, data = KidsFeet)
a numeric response by a two-level group (the t-tests in Ch 7)y ~ xt.test(length ~ sex, data = KidsFeet)

The ~ is read “by.” You will use these shapes for the whole course.


44. Describe & visualize (Ch 1–2)

GoalCodeProduces
Numerical summary of a variablefavstats(~ length, data = KidsFeet)min, Q1, median, Q3, max, mean, sd, n, missing
Same summary, by groupfavstats(length ~ sex, data = KidsFeet)one row of summaries per group
A single statisticmean(~ length, data=) · sd(...) · median(...) · IQR(...)one number (formula form)
Frequency table (categorical)tally(~ sex, data = KidsFeet)counts per category
Proportions instead of countstally(~ sex, data = KidsFeet, format = "proportion")shares per category
Two-way tabletally(~ sex + biggerfoot, data = KidsFeet)cross-tabulation
Histogramgf_histogram(~ length, data = KidsFeet)distribution of one numeric variable
Boxplot (optionally by group)gf_boxplot(length ~ sex, data = KidsFeet)center/spread/outliers across groups
Bar chart (categorical)gf_bar(~ sex, data = KidsFeet)counts per category

gf_* functions (the “ggformula” family) return plots you can label and color accessibly. R-Help Lesson 10 walks through titles, axis labels, and building an Okabe–Ito colorblind-safe palette step by step:

gf_boxplot(length ~ sex, fill = ~ sex, data = KidsFeet) %>%
  gf_labs(title = "Foot length by sex", x = "Sex", y = "Length (cm)") %>%
  gf_refine(scale_fill_manual(values = c("#0072B2", "#E69F00")))

55. Probability & the Normal model (Ch 3–4)

GoalCodeNotes
Normal area, with picturexpnorm(115, mean = 100, sd = 15)prints the z-score and both tail probabilities and shades the curve; matches the calculator’s normalcdf
Normal percentile (cutoff)xqnorm(0.90, mean = 100, sd = 15)the value with 90% below it; matches invNorm
Draw a Normal curve aloneplotDist("norm", mean = 100, sd = 15)the model on its own, no shading
Binomial probabilitydbinom(3, size = 10, prob = 0.4)exactly 3 successes; pbinom() for “≤ 3”
Build a sampling distribution (simulation)do(1000) * mean(~ length, data = resample(KidsFeet))resample-and-recompute; makes the Central Limit Theorem visible
Flip a coin / resample at randomrflip(10) · resample(x)the building blocks of a simulation

xpnorm() is the teaching tool of the two: it shows its work — the z-score, both tail areas, and the shaded curve — instead of returning a bare number. Set a seed (set.seed(1209)) before any do() simulation so your result is reproducible; see R-Help Lesson 11.


66. Inference: one proportion, one mean (Ch 5–7)

Pick the right procedure with the which-test guide. prop.test() and t.test()/tsum.test() all print a decision and a confidence interval in one call — you never run the interval and the test separately.

ProcedureCodeMatches (TI-83/84)Used in
One proportion, interval + testprop.test(x, n, p = 0.5)1-PropZInt / 1-PropZTestCh 6
One mean, raw data (t)t.test(~ length, data = KidsFeet, mu = 25)TInterval / T-TestCh 7
One mean from summary stats, σ\sigma unknown (t)tsum.test(mean.x = 24.72, s.x = 1.32, n.x = 39, mu = 25)TInterval / T-Test (Stats mode)Ch 7
One mean from summary stats, σ\sigma known (z)zsum.test(mean.x = 24.72, sigma.x = 1.3, n.x = 39, mu = 25)ZInterval / Z-TestCh 5 preview

Add alternative = "less" or alternative = "greater" to either prop.test() or a t-procedure for a one-sided test — the same directional choice you make on the calculator’s 1-PropZTest/T-Test screen.


77. Reading the table functions directly

When you want a raw probability or critical value without a full test (and want to check software against the distribution tables):

DistributionArea from a valueValue from an area
Normalpnorm(z)qnorm(p)
t (with df)pt(t, df)qt(p, df)

88. Reproducibility habits

HabitCodeWhy
Fix randomnessset.seed(1209)same simulation result every run
Comment your steps# what this line doesfuture-you and your grader thank you
Report your R versionsessionInfo()see R-Help Lesson 11