# Load package
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)
library(nycflights13)

Ch20 Vectors

Introduction

Vector Basics

Important types of automic vectors

Using atomic vectors

sample(10) + 10
##  [1] 20 17 13 16 11 19 14 18 12 15
1:10 + 1:2
##  [1]  2  4  4  6  6  8  8 10 10 12
1:10 + 1:3
## Warning in 1:10 + 1:3: longer object length is not a multiple of shorter object
## length
##  [1]  2  4  6  5  7  9  8 10 12 11
data.frame(a = 1:10, b = 1:2)
##     a b
## 1   1 1
## 2   2 2
## 3   3 1
## 4   4 2
## 5   5 1
## 6   6 2
## 7   7 1
## 8   8 2
## 9   9 1
## 10 10 2
# data.frame(a = 1:10, b = 1:3)
x <- 1:10
x
##  [1]  1  2  3  4  5  6  7  8  9 10
x[5]
## [1] 5
x[c(5, 7)]
## [1] 5 7
x[x>5]
## [1]  6  7  8  9 10

Recursive vectors (lists)

a <- list(a = 1:3, b = "a string", c = pi, d = list(-1, -5))
a
## $a
## [1] 1 2 3
## 
## $b
## [1] "a string"
## 
## $c
## [1] 3.141593
## 
## $d
## $d[[1]]
## [1] -1
## 
## $d[[2]]
## [1] -5
a[1:2]
## $a
## [1] 1 2 3
## 
## $b
## [1] "a string"
a[[4]]
## [[1]]
## [1] -1
## 
## [[2]]
## [1] -5
a[[4]][2]
## [[1]]
## [1] -5
a[[4]][[2]]
## [1] -5

Attributes

x <- 1:10
attr(x, "greeting")
## NULL
#> NULL
attr(x, "greeting") <- "Hi!"
attr(x, "farewell") <- "Bye!"
attributes(x)
## $greeting
## [1] "Hi!"
## 
## $farewell
## [1] "Bye!"

Augmented vectors

Ch21 Iteration

Introduction

For loops

# example from the cheatsheet
for (i in 1:4) {
    j <- i + 10
    print(j)
}
## [1] 11
## [1] 12
## [1] 13
## [1] 14
j
## [1] 14
# example 1: numeric calculation - add 10
x <- 11:15

for (i in seq_along(x)) {
    j <- x[i] + 10
    print(j)
} 
## [1] 21
## [1] 22
## [1] 23
## [1] 24
## [1] 25
# save output
y <- vector("integer", length(x))

for (i in seq_along(x)) {
    y[i] <- x[i] + 10
    print(y[i])
} 
## [1] 21
## [1] 22
## [1] 23
## [1] 24
## [1] 25
# example 2: string operation - extract first letter
x <- c("abc", "xyz")
y <- vector("character", length(x))

for (i in seq_along(x)) {
    y[i] <- x[i] %>% str_extract("[a-z]")
    print(y[i])
} 
## [1] "a"
## [1] "x"
# Output
y
## [1] "a" "x"

3 The map functions

# example 1: numeric calculation - add 10
x <- 11:15

y <- vector("integer", length(x))

for (i in seq_along(x)) {
    y[i] <- x[i] + 10
    print(y[i])
} 
## [1] 21
## [1] 22
## [1] 23
## [1] 24
## [1] 25
# Output
y
## [1] 21 22 23 24 25
# using map function with atomic vector
x
## [1] 11 12 13 14 15
map(.x = x, .f = ~.x + 10)
## [[1]]
## [1] 21
## 
## [[2]]
## [1] 22
## 
## [[3]]
## [1] 23
## 
## [[4]]
## [1] 24
## 
## [[5]]
## [1] 25
map_dbl(.x = x, .f = ~.x + 10)
## [1] 21 22 23 24 25
add_10 <- function(x) {x +10}
11 %>% add_10
## [1] 21
map_dbl(.x = x, .f = add_10)
## [1] 21 22 23 24 25
df <- tibble(
  a = rnorm(10),
  b = rnorm(10),
  c = rnorm(10),
  d = rnorm(10)
)

map_dbl(df, mean)
##           a           b           c           d 
## -0.06828501  0.46202750  0.26185692  0.07610149
map_dbl(df, median)
##          a          b          c          d 
## -0.4353763  0.4869967  0.2736563 -0.1809387
map_dbl(df, sd)
##         a         b         c         d 
## 1.0629285 0.6664766 0.7259644 0.9055626
df %>% map_dbl(mean)
##           a           b           c           d 
## -0.06828501  0.46202750  0.26185692  0.07610149
df %>% map_dbl(median)
##          a          b          c          d 
## -0.4353763  0.4869967  0.2736563 -0.1809387
df %>% map_dbl(sd)
##         a         b         c         d 
## 1.0629285 0.6664766 0.7259644 0.9055626

Dealing failure

Mapping over multiple arguments