MOBI BOOT CAMP CORP. logoLearning Buddy
  • SIGN IN
  • Introduction
  • Setup
  • 1A: Fundamental Building Blocks
  • 1B: Compound Statements
  • 2: Ordered Collection
  • 3: Key-Value Map and Structures
  • 4: More Data types
  • 5: Iteration Constructs
  • 6: Other constructs
  • 7. Regex
  • 8. Date and Time
  • Revision
  • Practice Exercise

Revision Cheat Sheet

In this section, you can review all the core R programming concepts you have learned in snippets of code.


1. Variables and Assignment

# Standard left-assignment
x <- 10

# Right-assignment
15 -> y

# Named keyword arguments in function calls (MUST use =)
mean(c(1, 2, NA), na.rm = TRUE)

2. Control Statements (if, ifelse, if_else)

score <- 650

# Multi-way branches
if (score >= 700) {
  status <- "Approved"
} else if (score >= 600) {
  status <- "Review"
} else {
  status <- "Denied"
}

# Base R inline (flexible types)
status_base <- ifelse(score >= 600, "Pass", 0)

# Tidyverse inline (strict type safety + NA handling)
library(dplyr)
status_tidy <- if_else(score >= 600, "Pass", "Fail", missing = "Unknown")

3. Loops and Best Practices

# For Loop (1-based index)
for (i in 1:3) {
  if (i == 2) next # Skip iteration
  cat("Iteration:", i, "\n")
}

# While Loop with simulation maxIter safety
max_iter <- 100
step <- 1
val <- 0
while (abs(val) < 5 && step <= max_iter) {
  val <- val + rnorm(1)
  step <- step + 1
  if (val > 4) break # Early exit
}

4. Custom Functions & Tidy Evaluation

# Custom function with default parameter
calculate_pct <- function(raw_val, digits = 2) {
  # Implicitly returns the last statement
  round(abs(raw_val) * 100, digits = digits)
}

# Tidy evaluation wrapper using embracing {{ }}
library(dplyr)
grouped_mean <- function(df, group_var, mean_var) {
  df |>
    group_by({{ group_var }}) |>
    summarize(avg = mean({{ mean_var }}, na.rm = TRUE))
}

5. Vectors and Vectorization

# Homogeneous vector creation
prices <- c(10, 20, 30)

# Element-wise vector multiplication
discounted <- prices * 0.90 # 9 18 27

# Logical vector filtering (Boolean Masking)
expensive <- prices[prices > 15] # 20 30

6. Lists and Named Collections

# Heterogeneous named list
student <- list(name = "Jane", age = 21, marks = c(85, 90, 88))

# Double bracket indexing (extracts actual value/content)
student_name <- student[[1]] # "Jane"

# Single bracket indexing (returns sub-list container)
name_sublist <- student[1] # list(name = "Jane")

# Dollar sign helper
student_age <- student$age # 21

7. Data Frames (Tabular Structures)

library(dplyr)
library(readr)

# Creating a modern tibble
df <- tibble(
  Product = c("A", "B"),
  Sales = c(150, 200)
)

# Row Bind (combine observations)
new_row <- tibble(Product = "C", Sales = 300)
df_extended <- rbind(df, new_row)

# Column Bind (combine variables)
df_final <- cbind(df_extended, Qty = c(10, 5, 2))

# Importing and Exporting CSV
# data <- read_csv("file.csv", skip = 1, na = c(".", "NA"))
# write_csv(df_final, "output.csv")

# Saving and loading native RDS
# write_rds(df_final, "df_final.rds")
# loaded_data <- read_rds("df_final.rds")

8. Strings, Factors, and stringr/forcats

library(stringr)
library(forcats)

# Combining strings (str_c propagates NA, paste0 turns it to "NA" text)
str_c("hello", NA) # NA
paste("hello", NA) # "hello NA"

# Subsetting from the end using negative offsets
str_sub("PROD_9982", -4, -1) # "9982"

# Splitting and flattening list outputs
parts <- unlist(str_split("A,B,C", ",")) # "A" "B" "C"

# Categorical Factors (ordered levels)
risk <- factor(c("Low", "High"), levels = c("Low", "High"), ordered = TRUE)

# Lumping small groups into "Other"
browsers <- factor(c("Chrome", "Chrome", "Opera", "Safari"))
lumped <- fct_lump_n(browsers, n = 1) # Chrome, Other

9. Date, Time, and lubridate

library(lubridate)

# Parser functions
d1 <- ymd("2026-06-20")
d2 <- parse_date_time("20/06/2026", orders = "dmy")

# Build date from numeric columns
dates <- make_date(year = 2026, month = 6, day = 20)

# Component extraction
year(d1)  # 2026
wday(d1, label = TRUE) # Sat

# Durations (exact physical seconds) vs. Periods (calendar clock time)
ddays(1) # "86400s (~1 days)"
days(1)  # "1d"

10. Regular Expressions (Regex)

library(stringr)

# Raw String literals (no double-escaping backslashes)
pattern <- r"(\d+)" # equivalent to "\\d+"

# Searching and extracting matches
is_match <- str_detect("User123", pattern) # TRUE
digits <- str_extract("User123", pattern)   # "123"

# Capture groups & backreferences (matching repeating characters)
repeat_pattern <- r"((.)\1)"
str_detect("apple", repeat_pattern) # TRUE ("pp")
Privacy Policy | Terms & Conditions