Function Cheatsheet
Week 2: Reading data into R
| Function | Example | What it does |
|---|---|---|
library |
library(tidyverse) |
Load an installed open-source R package. Run it once at the top of every notebook |
<- |
canlang_data <- read_csv("data/can_lang.csv") |
puts a result into a named object, so you can use it later |
read_csv |
read_csv("data/can_lang.csv") |
reads a comma separated file into a data frame |
skip |
read_csv("data/can_lang.csv", skip = 3) |
ignores the given number of lines at the top of the file |
col_names |
read_csv("data/can_lang.csv", col_names = FALSE) |
tells R whether the first row holds the column names, or gives your own names |
read_tsv |
read_tsv("data/can_lang.tsv") |
reads a file separated by tabs |
read_csv2 |
read_csv2("data/can_lang.csv") |
reads a file separated by semicolons |
read_delim |
read_delim("data/can_lang.tsv", delim = "\t") |
reads a file with a custom separator, use delim to specify the separator. delim = "\t" for tabs, delim = "," for commas, delim = ";" for semicolons, etc. |
read_excel |
read_excel("data/can_lang.xlsx", sheet = "Sheet1") |
reads one sheet from an Excel file. From readxl |
read_csv from a URL |
read_csv("https://.../can_lang.csv") |
reads a file straight from a web address, no download needed |
download.file |
download.file(url, "data/can_lang.csv") |
saves a file from the internet into your own folder first |
write_csv |
write_csv(ten_lang, "data/ten_lang.csv") |
save a data frame and export it as a .csv file |
getwd |
getwd() |
prints the folder R is working in right now |
rename |
rename(canlang_data, language = X1) |
gives a column a new name. New name on the left |
head |
head(canlang_data, 10) |
shows the first few rows |
tail |
tail(canlang_data, 6) |
shows the last few rows |
nrow |
nrow(canlang_data) |
counts the number of rows in a data frame |
? |
?read_csv |
opens the help page for a function |
dbConnect |
dbConnect(RSQLite::SQLite(), "data/can_lang.db") |
opens a connection to a database. From DBI |
dbListTables |
dbListTables(canlang_conn) |
lists the tables inside that database |
tbl |
tbl(canlang_conn, "lang") |
points at one table in the database. Nothing is loaded into R yet |
collect |
tbl(canlang_conn, "lang") |> collect() |
pulls the rows out of the database and into R for real |
show_query |
show_query(tbl(canlang_conn, "lang")) |
shows you the SQL that R wrote and sent for you |
Optional: scraping web pages and APIs
These are optional reading. You will not be tested on them.
| Function | Example | What it does |
|---|---|---|
read_html |
read_html("https://en.wikipedia.org/wiki/Canada") |
downloads a web page’s source code. From rvest |
html_nodes |
html_nodes(page, "td:nth-child(8)") |
picks out the pieces of the page that match a CSS selector |
html_text |
html_text(population_nodes) |
throws away the HTML tags and keeps the text |
request |
request("https://api.nasa.gov/planetary/apod?api_key=KEY") |
builds a query to an API. From httr2 |
req_perform |
req_perform(req) |
actually sends that query and waits for the answer |
resp_body_json |
resp_body_json(resp) |
turns the answer into an R list you can work with |
read_json |
read_json("data/nasa.json") |
reads a saved JSON file into an R list |
Week 3: Cleaning and wrangling data
| Function | Example | What it does |
|---|---|---|
pivot_longer |
pivot_longer(lang_wide, cols = Toronto:Edmonton, names_to = "region", values_to = "mother_tongue") |
many columns become two: one for the name, one for the value |
pivot_wider |
pivot_wider(lang_long, names_from = type, values_from = count) |
one column becomes many |
separate |
separate(lang_messy, col = value, into = c("most_at_home", "most_at_work"), sep = "/") |
splits one column into two based on a character |
select |
select(tidy_lang, language, region) |
keeps the columns you want, drops the rest |
: |
select(tidy_lang, language:most_at_home) |
keeps every column from one to another |
- |
select(tidy_lang, -region) |
keeps every column except that one |
starts_with |
select(tidy_lang, starts_with("most")) |
keeps columns whose name begins with that text. ends_with keeps columns whose name ends with that text. |
contains |
select(tidy_lang, contains("_at_")) |
keeps columns with that text anywhere in the name |
matches |
select(tidy_lang, matches("^most")) |
keeps columns whose name fits a pattern. |
filter |
filter(tidy_lang, region == "Toronto") |
keeps the rows that match a condition |
== and != |
filter(tidy_lang, category != "Official languages") |
equal to, and not equal to |
> and < |
filter(tidy_lang, most_at_home > 10000) |
above or below a threshold. Also >= and <= |
& |
filter(tidy_lang, (region == "Toronto") & (most_at_home > 10000)) |
“and”, both conditions have to be true |
| |
filter(tidy_lang, region == "Toronto" | region == "Montréal") |
at least one of the conditions has to be true |
%in% |
filter(tidy_lang, region %in% c("Toronto", "Montréal")) |
keeps rows whose value is in the list of values we are looking for |
! |
filter(tidy_lang, !(region %in% c("Toronto"))) |
flips a condition into its opposite |
mutate |
mutate(tidy_lang, prop = most_at_home / 35151728) |
makes a new column, or changes one that is already there |
as.numeric |
mutate(tidy_lang, most_at_home = as.numeric(most_at_home)) |
turns text that holds numbers into real numbers. Also as.integer |
arrange |
arrange(selected_lang, desc(mother_tongue)) |
sorts the rows by a column. desc() sorts the other way |
slice |
slice(arranged_lang, 1:10) |
keeps rows by their position |
pull |
pull(region_lang, most_at_home) |
takes one column out as a plain vector, not a data frame |
|> |
region_lang |> filter(region == "Toronto") |> select(language) |
pipe, hands the thing on the left to the function on the right |
summarize |
summarize(region_lang, min_most_at_home = min(most_at_home)) |
collapses many rows into one number |
group_by |
group_by(region_lang, region) |
cluster the rows into groups, so the next summarize calculate the stats (e.g., mean, max, min) for each group |
na.rm |
summarize(region_lang, max = max(most_at_home, na.rm = TRUE)) |
tells a summary to skip the missing values |
across |
summarize(region_lang, across(mother_tongue:lang_known, max)) |
runs the same summary on many columns at once |
map |
map(select(region_lang, mother_tongue:lang_known), max) |
runs the same function over every column, and gives back a list |
map_dfr |
map_dfr(select(region_lang, mother_tongue:lang_known), max) |
the same, stacked into a data frame. Also map_chr, map_dbl, map_int, map_lgl, map_dfc |
rowwise |
rowwise(region_lang) |
makes the next mutate work one row at a time, across the columns |
typeof |
typeof(region_lang$most_at_home) |
tells you what kind of thing you are holding. Also class |
Week 4: Effective data visualization
| Function | Example | What it does |
|---|---|---|
ggplot |
ggplot(can_lang, aes(x = most_at_home, y = mother_tongue)) |
starts the plot and says which column maps to which axis |
aes |
aes(x = size, y = landmass, fill = is_canada) |
maps a column to something you can see: an axis, a colour, a shape, a size |
geom_point |
+ geom_point() |
a scatter plot: the relationship between two quantitative variables |
geom_line |
+ geom_line() |
a line plot: how one quantitative variable changes over time |
geom_bar |
+ geom_bar(stat = "identity") |
a bar plot: amounts compared across categories |
geom_histogram |
+ geom_histogram(binwidth = 25) |
a histogram: how one quantitative variable is spread out |
geom_vline |
+ geom_vline(xintercept = 792.458, linetype = "dashed") |
a vertical reference line. geom_hline(yintercept = ) for a horizontal one |
labs |
+ labs(x = "Size (1000 square mi)", y = "Landmass", color = "Category") |
axis labels, legend titles and units, all in one place |
xlab |
+ xlab("Year") |
just the x axis label. ylab() for the y axis |
ggtitle |
+ ggtitle("Mauna Loa CO2 over time") |
a title above the plot |
theme |
+ theme(text = element_text(size = 20)) |
changes the look of everything that is not the data: text, legend, background, grid |
element_text |
element_text(size = 20) |
the size, face and colour of text inside theme() |
theme_classic |
+ theme_classic() |
a clean look: no grey panel, no grid lines |
facet_grid |
+ facet_grid(rows = vars(Expt)) |
one small plot per group, all on the same scale |
facet_wrap |
+ facet_wrap(~ Expt) |
the same idea, wrapped onto several lines |
vars |
facet_grid(rows = vars(Expt)) |
names the column to facet by, inside facet_grid() |
scale_x_log10 |
+ scale_x_log10() |
squashes a long tailed axis so the small values spread out. Also scale_y_log10() |
label_comma |
+ scale_x_log10(labels = label_comma()) |
formats axis numbers with thousands separators, so 1000000 reads as 1,000,000. From scales |
scale_x_continuous |
+ scale_x_continuous(breaks = seq(0, 100, 20)) |
choose where the tick marks go. Also scale_y_continuous() |
scale_color_brewer |
+ scale_color_brewer(palette = "Set2") |
select a color platte |
scale_fill_manual |
+ scale_fill_manual(values = c("steelblue", "darkorange")) |
pick the fill colours yourself, one per group |
display.brewer.all |
display.brewer.all(colorblindFriendly = TRUE) |
shows you every palette you can choose from. From RColorBrewer |
as_factor |
aes(fill = as_factor(Expt)) |
treats a column as a category instead of a number, so its values become groups |
fct_reorder |
aes(y = fct_reorder(landmass, size, .desc = TRUE)) |
reorders the values of a category by the values of another column |
reorder |
aes(x = reorder(language, mother_tongue)) |
the base R version of the same idea |
slice_max |
slice_max(islands_df, order_by = size, n = 12) |
keeps the rows with the largest values. slice_min() for the smallest |
unite |
unite(co2_df, col = "date_measured", year:month, sep = "-") |
glues several columns into one, joined by a character you choose |
ym |
mutate(co2_df, date_measured = ym(date_measured)) |
turns text like 1980-02 into a real date. From lubridate |
ggsave |
ggsave("img/faithful_plot.png", faithful_plot) |
saves the plot to a file. .png for photos and plots, .svg stays sharp at any size |
Week 5: Version control
| Git terminology | What it does | From → to |
|---|---|---|
| repository | a folder Git watches | |
| clone | copy a repository from GitHub, once | GitHub → local computer |
| add | choose changes for the next snapshot | local computer → staging area |
| commit | save a snapshot with a message | staging area → .git |
| push | send your snapshots to GitHub | .git → GitHub |
| pull | get new snapshots from GitHub | GitHub → .git and local computer |
| personal access token | the password GitHub wants when you push |
Week 6: Classification I
| Idea | In words | In R |
|---|---|---|
| classification | predict a category for a new observation | set_mode("classification") |
| distance | straight line between two points | computed for us |
| K-nearest neighbours | keep the K nearest, majority vote | nearest_neighbor(neighbors = K) |
| standardize | subtract the mean, divide by the sd | step_center(), step_scale() |
| workflow | recipe and model together | workflow(), fit(), predict() |
Functions
| Function | Example | What it does |
|---|---|---|
glimpse |
glimpse(cancer) |
one line per column: its name, its type, its first few values |
distinct |
distinct(cancer, Class) |
shows every different value a column takes, once each |
fct_recode |
mutate(cancer, Class = fct_recode(Class, "Malignant" = "M", "Benign" = "B")) |
renames the values of a category. New name on the left |
as_factor |
mutate(cancer, Class = as_factor(Class)) |
tells R a column of text is a category, not free text |
drop_na |
drop_na(missing_cancer) |
throws away every row with a missing value in it |
sqrt |
sqrt((a1 - b1)^2 + (a2 - b2)^2) |
the square root of a number |
slice_min |
slice_min(cancer, dist_from_new, n = 5) |
keeps the n rows with the smallest values in a column |
slice_head |
slice_head(cancer, n = 3) |
keeps the first few rows |
set.seed |
set.seed(1) |
fixes the dice roll, so a random result comes out the same tomorrow |
recipe |
recipe(Class ~ Area + Smoothness, data = cancer_train) |
says what you are predicting, from what, and with which data |
all_predictors |
step_scale(all_predictors()) |
a shortcut for every predictor column. Also all_numeric(), all_nominal(), all_outcomes() |
step_center |
step_center(all_predictors()) |
shifts each predictor so its mean is 0 |
step_scale |
step_scale(all_predictors()) |
stretches each predictor so its standard deviation is 1 |
step_normalize |
step_normalize(all_predictors()) |
centering and scaling in one step |
step_impute_mean |
step_impute_mean(all_predictors()) |
fills a missing value with that column’s average instead of dropping the row |
step_upsample |
step_upsample(Class, over_ratio = 1, skip = FALSE) |
copies the rare class until the two classes are balanced. From themis |
prep |
prep(uc_recipe) |
works out from the data whatever numbers the recipe’s steps need |
bake |
bake(uc_recipe, unscaled_cancer) |
applies a prepared recipe to a data set and hands back the transformed data |
nearest_neighbor |
nearest_neighbor(weight_func = "rectangular", neighbors = 7) |
the K-nearest neighbours model, and how many neighbours to use |
set_engine |
set_engine("kknn") |
which package does the actual computing underneath |
set_mode |
set_mode("classification") |
sets what kind of answer the model gives: a category, or a number |
workflow |
workflow() |
an empty holder you put a recipe and a model into |
add_recipe |
add_recipe(uc_recipe) |
puts the recipe into the workflow |
add_model |
add_model(knn_spec) |
puts the model into the workflow |
fit |
fit(data = cancer_train) |
runs the whole workflow on the training data |
predict |
predict(knn_fit, new_observation) |
gives a prediction for new rows it has never seen |
tibble |
tibble(Area = c(500, 1500), Smoothness = c(0.075, 0.1)) |
builds a small data frame by hand, one column per argument |
🚨 Common error messages
The message is R telling you what it tried to do and where it got stuck. Read the first line, then look at the line number.
| Message | What it usually means | Fix |
|---|---|---|
could not find function "read_csv" |
the package is not loaded in this session | run library(tidyverse) first |
object 'penguins' not found |
you did not run the line of code where the object ‘penguins’ was created, or you did not write that line of code, or you made a spelling mistake either when you use that object or when you created it | run the earlier cell, and check your spelling |
No such file or directory |
the path is wrong, or you are in a different folder than you think | check getwd(), then the folder names in the path |
object 'species' not found inside filter() |
a column name is misspelled, or it needs quotes because it is a value, not a column | column names bare, text values in quotes |
unexpected symbol / unexpected ')' |
a typo in the code itself: a missing comma, bracket, or quote | look at the line above the one R names too |
Error in ... : argument "x" is missing |
a function did not get an input it needs | check the function’s arguments |
non-numeric argument to binary operator |
you did arithmetic on text | the column read in as text, check it with glimpse() |
Where to read more
The textbook, Data Science: A First Introduction, has a chapter behind every week above.