# Vectors and iteration ## Learning objectives * Review the major types of vectors * Demonstrate how to subset vectors * Demonstrate vector recycling * Define lists * Demonstrate iterative operations using loops and `map()` functions * Practice writing iterative operations using loops and `map()` functions ## Notes April 29, 2020 ## Vectors * R stores all of its data in an object called a **vector** * Two types * Atomic vectors * List objects ## Atomic Vectors ### Logical Vectors * `TRUE`, `FALSE`, or `NA` (missing value) ### Numeric Vectors * Integers (whole numbers) * Doubles (numbers with decimal points) ### Character Vectors * Contain strings Atomic vectors are homogenous. You can't mix differet kinds of vectors ## Scalars * Scalars are a single number; vectors are a set of multiple values * Vector of length one ## Vector Recycling * `tidyverse` requires you do implicitly recycle a vector of shorter length. Base R does not ## Subsetting * To filter a vector, we cannot use `filter()` because that only works for filtering rows in a tibble. `[` is the subsetting function for vectors. It is used like `x[a]`. * With positive integers * With negative integers * _Don't mix_ positive and negative ### Subset with a logical vector * Subsetting with a logical vector keeps all values corresponding to a `TRUE` value. * Function `is.na()` that checks whether or not an observation is a missing value * These technique are for subsetting **a single vector** ## List * Created differently and have different properties than an atomic vector ### List `str()` `str(x)` ### Store a mix of objects * Lists can store different kinds of atomic vectors * Lists can store lists ### Nested Lists, etc. ### Subsetting Lists! ## Iterations ### `for()` loop * Helpful, but not intuitive * Easier to make a mistake ### Map functions * `purrr()` * More condensed * Most only require two arguments