1/64
Looks like no tags are added yet.
What does <- do in R?
assigns the value on the right to the object name on the left
What is a data frame
table with rows and columns
What is a function in R
a command that takes inputs and returns an output
The ones built into R, not made yourself e.g. tibble
What is a code chunk in a Quarto .qmd file
a block where you write R code that Quarto runs when rendering, and it shows the results (numbers/tables/plots) in the output.
What does Render do in Quarto?
runs all code chunks and converts the .qmd into an output document (e.g., HTML/PDF) with the results inserted
What is a package in R?
a collection of functions, data, and documentation that extends the basic capabilites of R
e.g. tidyverse
What does ```{r} mean in Quarto?
starts an R code chunk
What does #| eval: false do?
tells Quarto not to run this code chunk when rendering
What does library(readr) do?
It loads the readr package so you can use functions from it (including read_csv())
What does class() do
Tells you what type of object something is
Use it as quick sanity check right after importing data, to confirm R read it in the way you expected
e.g.
x <- 10 class(x) # "numeric"
What does names() do
Lists the column/variable names of a data frame
e.g. names(x) <- value
What does head() do
hows just the first few rows of a data frame (6 rows, by default)
preview your data without flooding the screen, handy for datasets with hundreds or thousands of rows
head(variable)
nrow()
Counts how many rows a data frame has
nrow(variable)
ncol()
Counts how many columns a data frame has
ncol(variable)
any()
Checks a condition across a whole column of values and returns a single TRUE if at least one value meets it, or FALSE if none do
e,g,
variable ←
completed = any(variable == "confirm")
sum()
adds up every value in a numeric column to give a single total.
e.g. sum(1, 2, 3, 4, 5, na.rm = FALSE)
ggplot()
Starts a new plot and tells R which dataset to build it from
aes()
Maps columns in your data to visual features of the plot
labs()
Adds or changes the text labels on a plot, such as the axis titles and the main title.
bins
how many bins to split the data into (for histogram)
facet_wrap()
Splits one plot into a grid of smaller plots, one panel per category.
geom_point()
Draws a scatter plot, one dot for every row of data
geom_smooth()
Adds a fitted trend line through your data.
method
the type of line to fit for trendline, for example "lm" for a straight regression line.
se
whether to show a shaded confidence band around the trendline. Set to FALSE to hide it.
alpha
the clarity of the points on scatter plot
scale_x_continuous()
Controls how the x-axis of a plot is drawn
trans
a transformation to apply to the axis, for example "log10"
labels
how to format the axis numbers, for example
scale_fill_okabe_ito()
Applies a colorblind-friendly set of colors to whatever is mapped to fill in your plot
theme_bw()
Switches the plot to a clean black-and-white theme
fct_infreq()
Reorders the categories of a variable by how often they occur, from most common to least common
fct_infreq(variable)
after_stat()
function used to map aesthetics to variables that are calculated internally by a statistical transformation (a "stat") rather than using raw columns from your dataset
dplyr
A package for wrangling data
filter()
Keeps only the rows that meet a condition you set, and drops the rest
filter(year %in% c(2022, 2023, 2024))
mutate()
Creates a new column in your data, or changes an existing one, based on pre-existing columns
Build new variables
e.g. mutate(equity = assets - liabilities)
select()
Keeps only the columns you name, and drops the rest.
e.g. select(variable, variable 2, variable 3)
arrange()
Sorts the rows of your data by one or more columns
e.g. arrange(desc(variable))
slice_max()
Keeps only the rows with the highest values of a column you choose
e.g. slice_max(variable)
group_by()
Splits your data into groups based on the values of one or more columns, without changing what the data looks like on screen.
group_by(column name) |>
summarise()
Collapses many rows into a single summary row, using calculations you choose, such as an average
e.g.
summarise(
ave_roic = mean(roic, na.rm = true),
sd_roic = sd(roic, na.rm = TRUE)
)
slice_head()
Shows the first few rows of your data, in the order they currently appear
slice_head(variable)
n()
Counts how many rows are in the current group
e.g.
variable |>
n()
desc()
Flags a column so that arrange() sorts it from highest to lowest instead of lowest to highest.
pivot_longer()
lengthen “short and wide” datasets by stacking values into a single column
pivot_wider()
shorten “long and narrow” datasets by spreading values into multiple columns
What makes data tidy?
Every tidy dataset has the same structure
Makes common R operations easier
!is.na
Checks for values that are not missing
$
Selects 1 variable in a dataset
==
boolean: ensures values on left = values on right
c()
combines arguments into a vector
!
The not operator
left_join
Keeps rows of
variable keep
other variables with join key
Dplyr Verbs that operate on rows
filter
arrange
Dplyr Verbs that operate on columns
mutate
select
Dplyr Verbs that operate on groups
group_by
summarise
Code for joins
new variable <-
variable keep |>
type of join(other variable, by = join_by(join key))
inner_join
Keeps only rows from both datasets that have the join key
full_join
Completely joins both datasets together with nothing lost
anti_join
A filtering join
Keeps rows of first dataset that don’t match those of second dataset
as_tibble
converts other data formats to tibble
transmute()
Adds new columns to dataset and drops all unmentioned original columns
Different to mutate since it doesn’t keep original columns
Same as mutate(.keep = "none")
e.g. transmute(equity = assets - liabilties)
coalesce()
finds the first non-missing value at each position
getSymbols
load and manage Symbols in specified environment
clean_names()
converts all column space to be lowercase, white space to underscores