c(1, 2, "three") # all become character
#> [1] "1" "2" "three"R Primer
The reference card: types, paths and the errors you will meet
The workshop assumes you are already sensitized to R and RStudio. The teaching happens in the five interactive primers, which run R in your browser with nothing installed:
| Primer | It teaches | Quiz | |
|---|---|---|---|
| 1 | Objects, functions and vectors | <-, calling a function, c(), one column of values |
Primer 1 |
| 2 | Tibbles | The data rectangle: rows are observations, columns are variables | Primer 2 |
| 3 | Packages and the native pipe | library(), and chaining steps with |> |
Primer 3 |
| 4 | The five dplyr verbs | filter(), select(), arrange(), mutate(), summarise(), and missing values |
Primer 4 |
| 5 | Your first plot | ggplot(): data, aesthetics, geom |
Primer 5 |
Do those first. Allow about 45 minutes for all five.
This page is the reference card for afterwards. It holds the four things the primers do not: the type table, how to read a file from disk, the errors you will meet, and where to read more.
The browser primers cannot teach you file paths or projects, because there are no files in a browser. Once R and RStudio are installed, retype a few of the primer cells in your own console. Typing is slower than copy-paste, and that is the point: you notice the brackets.
The four types you will meet
A vector, and therefore a tibble column, holds only one type.
| Type | Example | In your data |
|---|---|---|
numeric |
72.5 |
Marks, age, BMI |
character |
"MBBS" |
Names, comments |
logical |
TRUE |
Pass/fail flags |
factor |
Female |
Categories with fixed levels |
Mixing types does not raise an error. R silently converts everything to the most permissive type, which is a classic source of confusion:
str() is the function to reach for when something goes wrong. Nine times out of ten the problem is a column that is character when you thought it was numeric.
The primers say tibble; older books and most online answers say data frame. A tibble is a data frame, with better printing and fewer surprises. data.frame() is base R; tibble() comes from the tidyverse. Everything in this workshop works on both.
Getting data in
library(tidyverse)
library(here)
students <- read_csv(here("data", "meded_students.csv"))A relative path, because you are working inside an RStudio project. here() builds the path from the project folder, so the same line works on your laptop and on your colleague’s. See Datasets for the files.
setwd()
setwd("C:/Users/arun/Desktop/analysis") is the one line guaranteed to break your script on any other computer. An RStudio project plus here() replaces it.
Errors you will meet, and what they mean
R is case sensitive. Most beginner errors are a capital letter or a bracket.
| Message | Cause | Fix |
|---|---|---|
could not find function "read_csv" |
Package not loaded | library(readr), or library(tidyverse) which includes it |
there is no package called 'readr' |
Package not installed | install.packages("readr"), in the console, once |
object 'viva' not found |
Typo, or you never ran the line that created it | Re-run the earlier line; check spelling and case |
unexpected symbol / unexpected ')' |
Missing comma or unbalanced bracket | Look at the line above the one flagged |
+ in the console instead of > |
R is waiting for you to finish an expression | Press Esc and retype |
A result of NA |
A missing value upstream | Count them with sum(is.na(x)), then decide: na.rm = TRUE |
Read the message. It names which of the two things is missing, the function or the object, and that tells you whether to load a package or fix a typo.
Handy shortcuts
| Shortcut | Does |
|---|---|
| Alt + - | Types <- |
| Ctrl + Shift + M | Types the pipe. It gives |> only if you ticked Use native pipe operator during setup |
| Ctrl + Enter | Runs the current line |
| Ctrl + Shift + F10 | Restarts R (a clean session) |
| Tab | Completes a name, and shows the arguments |
Where to go if you want more
- R for Data Science (2e), chapters 2–4: free online, the standard reference
- Posit primers: interactive, browser-based, no installation
- Swirl: learn R inside R,
install.packages("swirl"); library(swirl); swirl()