show/hide
percent_of_income <- function(expense, income) {
(expense / income) * 100
}
percent_of_income(expense = 1500, income = 5000)
#> [1] 30A quick reference to the R concepts used throughout this book.
Functions perform operations (calculate, transform, model, graph) on various objects that contain information (blood pressure measurements, monthly sales, political party affiliation, etc.)
percent_of_income <- function(expense, income) {
(expense / income) * 100
}
percent_of_income(expense = 1500, income = 5000)
#> [1] 30The pattern is name <- function(inputs) { body }. The last expression in the body is returned automatically; return() is optional.
Vectors are one-dimensional containers where every value has the same type.
The four atomic types: logical, integer, double, and character.
c(TRUE, FALSE, TRUE) # logical
#> [1] TRUE FALSE TRUE
c(1L, 2L, 3L) # integer
#> [1] 1 2 3
c(1.5, 2.0, 3.14) # double
#> [1] 1.50 2.00 3.14
c("apple", "banana", "cherry") # character
#> [1] "apple" "banana" "cherry"S3 vectors (factors, dates, date-times, durations) are atomic vectors with extra class metadata that gives them specialized behavior.
factor(c("low", "med", "high"),
levels = c("low", "med", "high")) # factor
#> [1] low med high
#> Levels: low med high
as.Date("2026-01-15") # date
#> [1] "2026-01-15"
as.POSIXct("2026-01-15 09:30:00") # date-time
#> [1] "2026-01-15 09:30:00 MST"
as.difftime(2, units = "hours") # duration
#> Time difference of 2 hoursTwo dimensional objects. All values must share the same atomic type.
matrix(1:6, nrow = 2, ncol = 3)
#> [,1] [,2] [,3]
#> [1,] 1 3 5
#> [2,] 2 4 6Multidimensional objects. Same-type values; dimensions defined by dim().
array(1:12, dim = c(2, 3, 2))
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] 1 3 5
#> [2,] 2 4 6
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] 7 9 11
#> [2,] 8 10 12Rectangular objects: each column is a vector, and columns can be different types. Tibbles are tidyverse-style data frames with nicer printing and stricter behavior.
tibble::tibble(
name = c("Alice", "Bob", "Carol"),
age = c(34L, 28L, 41L),
paid = c(TRUE, FALSE, TRUE)
)
#> # A tibble: 3 × 3
#> name age paid
#> <chr> <int> <lgl>
#> 1 Alice 34 TRUE
#> 2 Bob 28 FALSE
#> 3 Carol 41 TRUERecursive objects: each element can be any object, including another list. The most flexible container in R, and the building block underneath data frames, model objects, and JSON-like data.
list(
numbers = c(1, 2, 3),
name = "Alice",
flags = c(TRUE, FALSE),
nested = list(a = 1, b = "two")
)
#> $numbers
#> [1] 1 2 3
#>
#> $name
#> [1] "Alice"
#>
#> $flags
#> [1] TRUE FALSE
#>
#> $nested
#> $nested$a
#> [1] 1
#>
#> $nested$b
#> [1] "two"