Skip to main content

R Quick Reference {.unnumbered}

Every R function used in this workbook, organized by task. Use this as a lookup table when you’re working on assignments.

Steps on this page

Importing Data

Function Package What It Does Example
read_csv() tidyverse Imports a CSV file into R df <- read_csv("data.csv")
read_rds() tidyverse Loads a saved .RDS file df <- read_rds("data.RDS")
clean_names() janitor Converts column names to lowercase_with_underscores df <- df %>% clean_names()
as_datetime() lubridate Turns a count of seconds into a date-time; divide epoch milliseconds by 1,000 first mutate(ts = as_datetime(date / 1000))

Exploring Data

Function Package What It Does Example
glimpse() tidyverse Shows every column with data type and preview glimpse(df)
summary() base R Summary statistics for all columns summary(df)
dim() base R Number of rows and columns dim(df)
names() base R List column names names(df)
nrow() base R Count rows nrow(df)
head() base R Show first 6 rows head(df)

Cleaning Data

Function Package What It Does Example
distinct() tidyverse Remove duplicate rows df %>% distinct()
filter() tidyverse Keep rows matching a condition df %>% filter(age > 18)
mutate() tidyverse Create or modify columns df %>% mutate(new = old * 2)
case_when() tidyverse Recode values with if/then logic case_when(x == 1 ~ "yes", TRUE ~ "no")
str_to_lower() tidyverse Convert text to lowercase str_to_lower("POP") → "pop"
is.na() base R Check for missing values filter(!is.na(column))
left_join() with closest() tidyverse Attaches each message’s nearest earlier stream snapshot (a rolling join) left_join(chat, snapshots, by = join_by(channel, closest(ts >= snapshot_ts)))
reliability() v2v Cohen’s kappa or Krippendorff’s alpha from two columns of codes reliability(pull(pilot, pass_1), pull(pilot, pass_2), method = "kappa")
factor() base R Convert column to a categorical factor factor(df$genre)
levels() base R Show factor levels levels(df$genre)

Counting and Summarizing

Function Package What It Does Example
count() tidyverse Count rows by group df %>% count(genre, sort = TRUE)
group_by() tidyverse Group data for per-group calculations df %>% group_by(genre)
summarize() tidyverse Calculate summary statistics per group summarize(mean_val = mean(x))
ungroup() tidyverse Remove grouping df %>% ungroup()
pivot_wider() tidyverse Reshape long → wide pivot_wider(names_from = col, values_from = n)

Visualization (ggplot2)

Function What It Does Example
ggplot() Initialize a plot ggplot(df, aes(x = genre))
aes() Map variables to visual properties aes(x = genre, fill = mode)
geom_col() Bar chart (from pre-counted data) + geom_col()
geom_bar() Bar chart (counts from raw data) + geom_bar()
geom_boxplot() Boxplot + geom_boxplot()
geom_text() Add text labels to a plot + geom_text(aes(label = n))
coord_flip() Flip x and y axes (horizontal bars) + coord_flip()
fct_reorder() Sort factor levels by a variable fct_reorder(genre, n)
fct_infreq() Sort factor levels by frequency fct_infreq(genre)
scale_fill_brewer() Colorblind-friendly color palette + scale_fill_brewer(palette = "Set2")
labs() Add title, subtitle, axis labels, caption + labs(title = "My Chart")
theme_minimal() Clean, minimal chart theme + theme_minimal()
v2v::theme_v2v() The course chart theme + v2v::theme_v2v()
v2v::scale_fill_v2v() The course fill palette (up to 8 colors) + v2v::scale_fill_v2v()
ggsave() Save the last chart to a file ggsave("figures/fig1.png", width = 7, height = 5)

Statistical Testing

Function Package What It Does Example
table() base R Build a contingency table table(df$var1, df$var2)
prop.table() base R Convert counts to proportions prop.table(tbl, margin = 1)
chisq.test() base R Chi-square test of independence chisq.test(tbl)
$statistic n/a Extract χ² value from test result result$statistic
$parameter n/a Extract degrees of freedom result$parameter
$p.value n/a Extract p-value result$p.value
$stdres n/a Extract standardized residuals result$stdres
xtabs() base R Build a contingency table from a formula xtabs(~ cat_a + cat_b, data = df)
broom::tidy() broom Turn a test or model result into a one-row-per-term table t.test(y ~ g, data = df) %>% broom::tidy()
broom::glance() broom One row of model-level numbers, including nobs fit %>% broom::glance()
t.test() base R Welch t-test of a numeric outcome across two groups t.test(y ~ g, data = df)
effectsize::cohens_d() effectsize Cohen’s d with its interval effectsize::cohens_d(y ~ g, data = df)
effectsize::cramers_v() effectsize Cramér’s V for a contingency table effectsize::cramers_v(tbl, adjust = FALSE)
aov() base R One-way ANOVA, then summary() for the F table summary(aov(y ~ g3, data = df))
lm() base R Linear regression, then summary() for the coefficients summary(lm(y ~ x, data = df))
cor.test() base R Pearson correlation with its interval cor.test(~ x + y, data = df)
$expected n/a Extract the expected counts under the null hypothesis round(result$expected, 1)
effectsize::eta_squared() effectsize Eta squared for an aov() fit, with its interval effectsize::eta_squared(fit)
effectsize::omega_squared() effectsize Omega squared, the less biased cousin of eta squared effectsize::omega_squared(fit)
oneway.test() base R Welch’s ANOVA, which drops the equal-spread assumption oneway.test(y ~ g3, data = df)
TukeyHSD() base R Post-hoc pairwise comparisons after aov(), with adjusted p-values TukeyHSD(fit)
effectsize::standardize_parameters() effectsize Standardized slopes for an lm() fit effectsize::standardize_parameters(fit)

Exporting Data

Function What It Does Example
write_rds() Save data as .RDS (preserves factors and date-times) write_rds(df, "clean.RDS")
write_csv() Save data as CSV write_csv(df, "output.csv")

Publishing (Quarto)

Command What It Does
quarto render Render all chapters into PDF + HTML
knitr::kable() Format a data frame as a publication-ready table
@citationkey Insert an in-text citation from references.bib

The Pipe: %>%

The pipe passes the result from the left to the first argument of the function on the right:

clean_names(read_csv("data.csv"))

read_csv("data.csv") %>% clean_names()

chat %>%
  filter(!is.na(is_gaming)) %>%
  count(channel, sort = TRUE) %>%
  mutate(pct = n / sum(n) * 100)

Read %>% as “and then.”