📈 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.
Summary Table
Summary table of the R section.
Basics
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 |
Advanced
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 |