m1 <- matrix(1:6, nrow = 2, ncol = 3)
m1 [,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
GPH-GU 2182: Statistical Programming in R · Fall 2026
New York University · School of Global Public Health
2026-09-16
A matrix is a 2-dimensional collection of elements of the same basic type (homogeneous), arranged in rows and columns.
[row, column]Always remember: [row, column] (think “RC Cola” or “Roman Catholic”).
In your week-03-inclass.qmd worksheet:
icu_admissions containing the numbers c(12, 18, 5, 24, 30, 11, 8, 15, 4) filled row-by-row.c("Mon", "Tue", "Wed") and colnames: c("Site_1", "Site_2", "Site_3").colSums(icu_admissions).Site_2 admissions exceeded 15. Site_1 Site_2 Site_3
Mon 12 18 5
Tue 24 30 11
Wed 8 15 4
Site totals:
Site_1: 44, Site_2: 63, Site_3: 20
Time Remaining: 10 minutes.
Vectors and matrices are homogeneous (all elements must be the exact same type).
In public health and clinical research, real records contain heterogeneous data:
A list is a flexible container whose elements can be of any type, length, or structure!
List of 5
$ id : chr "PT-84920"
$ age : num 54
$ smoker : logi FALSE
$ sbp_history: num [1:5] 138 142 130 128 135
$ metadata :List of 2
..$ clinic : chr "Bellevue"
..$ enrolled: chr "2026-01-15"
$ vs [[ ]] vs [ ]$ or [[ ]] (Extract Contents)Extracts the raw object inside (here a numeric vector, so math works):
Think of a list as a train with cargo cars. [4] gives you the 4th car; [[4]] opens the car and gives you the cargo inside!
In week-03-inclass.qmd:
study_subject containing:
name: "Jordan Lee"arm: "Treatment_A"viral_loads: c(45000, 12000, 850, 40)adverse_events: 0viral_loads vector and calculate the percentage reduction from baseline (visit 1 to visit 4).completed set to TRUE.Baseline: 45000
Final: 40
Reduction: 99.91%
$completed
[1] TRUE
Time Remaining: 10 minutes.
A data frame is a list of equal-length vectors. Each column is a variable; each row is an observation.
[1] 5
[1] 4
A tibble (tibble::tibble()) is a modern data frame with refined behavior:
data.framedf[, 1] becomes a vector).df$d might match df$diabetes).tibble (library(tibble))<chr>, <dbl>, <lgl>).tb[, 1] always stays a tibble.[1] 24.2 31.0 27.5 29.8 22.1
[1] 45 62 38 71 55
id age bmi diabetes bmi_high
1 P01 45 24.2 FALSE FALSE
2 P02 62 31.0 TRUE TRUE
3 P03 38 27.5 FALSE FALSE
4 P04 71 29.8 TRUE FALSE
5 P05 55 22.1 FALSE FALSE
id age bmi diabetes bmi_high
2 P02 62 31.0 TRUE TRUE
4 P04 71 29.8 TRUE FALSE
id age bmi diabetes bmi_high
3 P03 38 27.5 FALSE FALSE
In week-03-inclass.qmd:
patients data frame.pulse_pressure computed as sbp - dbp.pulse_pressure > 45bmi of this high-risk sub-cohort.patients[condition, ]mean(..., na.rm = TRUE) if missing values exist.readr)Once your data is cleaned and validated, save it back to disk:
[1] NA
[1] "character"
Why? Row 42 holds "Unknown", so read.csv made the whole column character!
Note
Remember: Your lowest quiz score of the semester is automatically dropped!