Language basics
20 steps in 6 sets of R.
R does not really have scalars. A single number is a vector of length one, and once that lands, a lot of R stops being surprising. This tour covers vectors and types, flow control and functions, then a set called Vectorized thinking that is the actual point.
Matrices and lists, text and dates after that. Twenty steps, and the habit it is trying to build is reaching for a whole-vector operation before reaching for a loop.
Vectors & types
- Vectors and indexingThe vector is R's fundamental unit; everything indexes it.
- Types and coercionR's atomic types and the rules that convert between them.
- Sequences and orderingGenerating and reordering regular vectors.
- FactorsR's type for categorical data, ordered or not.
Flow & functions
- ConditionalsBranching, and its vectorized alternative.
- FizzBuzzThe classic screening exercise, done the way R prefers: over a vector.
- Primality testTrial division, written vectorized so it tests a whole vector at once.
- Clamp valuesBounds a vector between a floor and a ceiling using pmin and pmax.
- Fibonacci vectorBuilds the sequence into a preallocated vector rather than growing one.
Vectorized thinking
- The apply familyR's idiomatic replacement for explicit loops.
- Column means with applyColumn means with apply, the family that replaces most loops in base R.
- Normalize a vectorRescales a vector to zero mean and unit variance, the transform every model wants.
- Binary searchHalve the range each step, with R's one-based indexing to watch.
- Moving averageA rolling mean with filter, and the NA padding at the edges.
Matrices & lists
Text & dates
- String operations in base RMeasuring, splitting, and rewriting character vectors.
- DatesParsing, formatting, and doing arithmetic on dates.
- Word frequencyWord frequencies with table, R's one-line counter.
Encore
- csv_summary.RA complete script: read a CSV, summarize each column, print a report.