R

📈 Welcome to the R Section! Whether you’re brand new to R or aiming to sharpen your advanced skills, this section has something for you. It’s organized into three levels—R Basics, Intermediate, and Advanced—each carefully designed to build your programming and data analysis expertise. From your first print() statement to writing efficient code with functions and advanced data wrangling, you’ll develop the confidence to solve real-world problems using R.

Installation

Please refer to the Data Science Setup to make sure you are up and running!

Summary Table

Summary table of the R section.

Basics

Concept Example Description
numeric / double 3.14, 10 Numbers (with or without decimals), stored as double
integer 10L Whole numbers with L suffix
character "GW" Text (strings)
logical TRUE, FALSE Boolean values
raw charToRaw("Tyler") Binary data in raw byte format
NULL NULL Represents no value
Assignment x <- 10, y = 3.14 Assign value to a variable
Naming value, .value, Value Variable names must follow rules and are case-sensitive
Reassignment x <- x + 1 Update variable value
Multiple Assignment a <- b <- c <- 0 Assign same value to multiple variables
Dynamic Typing x <- 5 then x <- "five" Variables can change type
vector c(95, 88, 90) One-dimensional, same type
list list(name = "Intro", code = 6101) Collection of different types
matrix matrix(c(1, 2, 3, 4), nrow = 2) 2D, same type structure
data.frame data.frame(name = "Alice", score = 95) Tabular structure with columns of possibly mixed types
factor factor(c("male", "female")) Categorical data with levels

Intermediate

Concept Example Description
One-Based Indexing vec[1] First element is at index 1
Negative Indexing vec[-1] Excludes the first element
Multi-Level Indexing lst$name[1], substr(x, 1, 1) Access nested or character-level elements
Slice Range vec[2:4] Inclusive range from index 2 to 4
Slice to End vec[3:length(vec)] Elements from index 3 to end
Full Slice vec[] All elements
Index Out of Bounds vec[10] Returns NA if index exceeds length
Mixed Positive/Negative vec[c(-1, 2)] Error if mixing negative and positive indices
if Statement if (x > 0) { ... } Runs block if condition is TRUE
else Statement else { ... } Runs if if condition is FALSE
else if Statement else if (x == 0) { ... } Adds more conditions
Logical Operators &, |, ! Combine or negate logical conditions
Nested Conditionals if (...) { if (...) { ... }} Place one if inside another
for Loop for (i in 1:5) { ... } Iterate over values in a vector
Loop over Characters for (i in seq_len(nchar(str))) Index through a string
Loop over List for (name in names(list)) { ... } Iterate over names and values in a list
while Loop while (x > 0) { ... } Continue loop while condition is TRUE
break if (x == target) break Exit loop early
next if (x == skip) next Skip current iteration
Infinite Loop while (TRUE) { ... } Runs forever unless manually stopped

Advanced

Concept Example Description
Basic function greet <- function() { print("Hi") } Define a reusable block of code
Parameters function(name) { print(name) } Accept input values into the function
Return values return(x + y) Send a result back to the caller
Default values function(name = "Guest") Provide fallback values for parameters
Named arguments fun(b = 2, a = 1) Pass arguments by name in any order
Variable arguments function(...) Accept any number of inputs
Access ... args <- list(...) Convert ... to a list of named arguments
Multiple return values return(list(sum = x + y, diff = x - y)) Return a list to simulate multiple return values
Missing required argument fun(x) → fun() Error if required arg isn’t provided
Missing parentheses function(x (syntax error) Missing closing ) breaks the function
Load a package library(ggplot2) Make all functions in a package available
Conditional loading if (require(pkg)) { ... } Load only if available; returns TRUE/FALSE
Aliased access dplyr::filter() Use function without loading full package
Load custom script source("utils.R") Run code from another R file
Access dataset data(mtcars) Load built-in dataset
View top rows head(mtcars) Show first 6 rows
Summary stats summary(df) Get mean, median, min, max, etc.
Handle NA na.omit(df) Remove missing data
Impute mean df$var[is.na(var)] <- mean(var, na.rm=TRUE) Replace NAs with average
Normalize (x - min(x)) / (max(x) - min(x)) Rescale between 0 and 1
Scale (standardize) scale(df) Mean = 0, SD = 1
Scatter plot ggplot(df, aes(x, y)) + geom_point() Basic scatterplot using ggplot2
Histogram geom_histogram(binwidth = 2) Show frequency distribution
Import error library(badpkg) Error if package doesn’t exist
Function not loaded select(df) without library(dplyr) Error if function isn’t available in namespace