---
title: "Base R Functions & Apply Family"
---
# Common Utilities
```{r}
x <- 1:10
sum(x); mean(x); sd(x); var(x); quantile(x)
seq(0, 1, by=0.1); rep(5, times=3)
```
# Apply Family
```{r}
m <- matrix(1:9, nrow=3)
apply(m, 1, mean) # row means
apply(m, 2, mean) # col means
lst <- list(a=1:3, b=10:12)
sapply(lst, mean) # simplifies result
mapply(sum, 1:3, 10:12)
```
# Subsetting Essentials
```{r}
df <- data.frame(id=1:3, val=c(10,20,30))
df[1, "val"]
df[df$val > 10, ]
```
# Exercises
1. Use `apply` to get the max per column of a numeric matrix.
2. Write a base R snippet to compute IQR for each column of `mtcars`.
3. Compare `lapply` vs `sapply` in behavior on a list with mixed types.