# ============================================================
# Subsetting & Indexing
# R Foundations — Rverse Analytics
# https://rverseanalytics.com/learn/subsetting
# ============================================================

# Pick elements out of a vector with [ ]
v <- c(10, 20, 30, 40, 50)
v[2]          # the second element
v[c(1, 3)]    # first and third
v[-1]         # everything except the first
v[v > 25]     # keep the ones that pass a test
# Name the elements, then pull by name
scores <- c(ali = 90, vera = 82, sam = 77)
scores["vera"]
# A data frame is indexed as [rows, columns]
head(mtcars[, c("mpg", "hp")])
mtcars[mtcars$mpg > 30, c("mpg", "cyl")]
# $ and [[ ]] pull a single column out
mtcars$mpg[1:5]
mtcars[["hp"]][1:5]
