R from the ground up
The language statisticians reach for when the data arrives
- Beginner
- 12 chapters
- 211 min read
Learn R by writing it: printing and arithmetic, vectors and indexing, truth and missing values, text, data frames, filtering and sorting rows, functions, loops and the apply family, grouped totals and joins, summary statistics and a fitted line, missing values and tidying, ending with a sales report built from all of it.
What You'll Learn
- Print output with cat, understand the index prefix print adds, and store values with the arrow
- Build vectors with c, the colon, seq and rep, and index them from one
- Write conditions with if and else, and test a whole vector with which, any and all
- Handle NA deliberately instead of letting it poison a total
- Reshape text with paste, sprintf, substr, strsplit, grepl and gsub
- Build a data frame from vectors and compute a new column from two others
- Filter rows with a logical test or subset, and sort them with order
- Write functions with defaults, and apply one to a vector with sapply and vapply
- Repeat work with for and while, and steer it with next and break
- Summarise per group with aggregate, count with table, and join two tables with merge
- Describe a column with mean, median and sd, and fit a straight line with lm
- Find and handle missing values, and tidy duplicates with unique, setdiff and intersect
- Write a formatted report: a table, a loop of sprintf lines, totals per group and a share
Prerequisites
- No programming experience needed — this course starts at the first line
- Any other language you know will make the early chapters quick
- No statistics background is assumed; the statistics chapter explains its own terms
Course Content
- 01Your first RNot started
What R is for, the two ways to print something, how the arrow stores a value, and the arithmetic every lesson after this one is built on.
- 02Vectors: R thinks in rows of valuesNot started
The vector is the one building block R is made of: how to build one, how to reach into it, and why arithmetic on a whole vector needs no loop.
- 03Truth, missing values and choicesNot started
Comparisons that answer TRUE or FALSE, tests over a whole vector, if and else, and NA — the third answer that means "not known".
- 04Text you can reshapeNot started
Gluing text together with paste and sprintf, measuring and slicing it, and taking it apart with strsplit, grepl, sub and gsub.
- 05Data frames: tables you can compute onNot started
The table shape R is built around: build one from named vectors, reach into a column with the dollar sign, add a computed column, and read it back.
- 06Choosing, sorting and filtering rowsNot started
Keeping the rows that pass a test, asking the same question with subset, picking columns, and sorting a table by one of them.
- 07Naming your own workNot started
Writing functions of your own: arguments with defaults, the value that comes back, and handing a function to sapply, vapply and Reduce.
- 08Loops, lists and the apply familyNot started
Repeating work with for and while, steering a loop with next and break, and knowing when lapply or sapply says the same thing better.
- 09Groups, counts and joined tablesNot started
Totals per group with aggregate, counts with table, and merge — the one call that brings a price list from another table into your orders.
- 10Summary statistics and one straight lineNot started
The five numbers worth knowing first, then lm: fitting a straight line to two columns and reading its intercept, slope and r squared.
- 11Missing values, duplicates and tidyingNot started
Finding the holes in a table with is.na, working around them with na.rm and filtering, and tidying duplicates with unique, setdiff and intersect.
- 12Capstone: a sales report in base RNot started
Everything at once: a small table, a computed column, one formatted report line per row, per-region totals, the best seller and a share of the total.
