exercises.Rmd
in the repo: github.com/malcolmbarrett/lawrug_purrrc(a = "hello", b = 1)
## a b ## "hello" "1"
list(a = "hello", b = 1, c = mean)
## $a## [1] "hello"## ## $b## [1] 1## ## $c## function (x, ...) ## UseMethod("mean")## <bytecode: 0x7fe91ec2c958>## <environment: namespace:base>
x <- list(a = "hello", b = 1)as.data.frame(x)
## a b## 1 hello 1
library(gapminder)head(gapminder$pop)
## [1] 8425333 9240934 10267083 11537966 13079460 14880372
gapminder[1:6, "pop"]
gapminder[1:6, "pop"]
## # A tibble: 6 x 1## pop## <int>## 1 8425333## 2 9240934## 3 10267083## 4 11537966## 5 13079460## 6 14880372
head(gapminder[["pop"]])
## [1] 8425333 9240934 10267083 11537966 13079460 14880372
sum(rnorm(10))
sum(rnorm(10))
## [1] -3.831574
sum(rnorm(10))
## [1] -3.831574
sum(list(x = rnorm(10), y = rnorm(10), z = rnorm(10)))
sum(rnorm(10))
## [1] -3.831574
sum(list(x = rnorm(10), y = rnorm(10), z = rnorm(10)))
## Error in sum(list(x = rnorm(10), y = rnorm(10), z = rnorm(10))): invalid 'type' (list) of argument
library(purrr)x_list <- list(x = rnorm(10), y = rnorm(10), z = rnorm(10))map(x_list, mean)
library(purrr)x_list <- list(x = rnorm(10), y = rnorm(10), z = rnorm(10))map(x_list, mean)
library(purrr)x_list <- list(x = rnorm(10), y = rnorm(10), z = rnorm(10))map(x_list, mean)
library(purrr)x_list <- list(x = rnorm(10), y = rnorm(10), z = rnorm(10))map(x_list, mean)
## $x## [1] -0.6097971## ## $y## [1] -0.2788647## ## $z## [1] 0.6165922
map()
with data framesgapminder %>% dplyr::select_if(is.numeric) %>% map(sd)
## $year## [1] 17.26533## ## $lifeExp## [1] 12.91711## ## $pop## [1] 106157897## ## $gdpPercap## [1] 9857.455
x <- x^2x <- scale(x)x <- max(x)
x <- x^2x <- scale(x)x <- max(x)y <- x^2y <- scale(y)y <- max(y)z <- z^2z <- scale(x)z <- max(z)
x <- x^2x <- scale(x)x <- max(x)y <- x^2y <- scale(y)y <- max(y)z <- z^2z <- scale(x)z <- max(z)
x <- x^3x <- scale(x)x <- max(x)y <- x^2y <- scale(y)y <- max(y)z <- z^2z <- scale(x)z <- max(z)
.f <- function(x) { x <- x^3 x <- scale(x) max(x)}.f(x).f(y).f(z)
map()
map()
## $country## [1] 142## ## $continent## [1] 5## ## $year## [1] 12## ## $lifeExp## [1] 1626## ## $pop## [1] 1704## ## $gdpPercap## [1] 1704
map | returns |
---|---|
map() |
list |
map_chr() |
character vector |
map_dbl() |
double vector (numeric) |
map_int() |
integer vector |
map_lgl() |
logical vector |
map_dfc() |
data frame (by column) |
map_dfr() |
data frame (by row) |
map_int(gapminder, ~length(unique(.x)))
## country continent year lifeExp pop gdpPercap ## 142 5 12 1626 1704 1704
means <- c(-3, 4, 2, 2.3)sds <- c(.3, 4, 2, 1)map2_dbl(means, sds, rnorm, n = 1)
means <- c(-3, 4, 2, 2.3)sds <- c(.3, 4, 2, 1)map2_dbl(means, sds, rnorm, n = 1)
means <- c(-3, 4, 2, 2.3)sds <- c(.3, 4, 2, 1)map2_dbl(means, sds, rnorm, n = 1)
## [1] -2.997932 2.178125 1.266952 2.948287
input 1 | input 2 | returns |
---|---|---|
map() |
map2() |
list |
map_chr() |
map2_chr() |
character vector |
map_dbl() |
map2_dbl() |
double vector (numeric) |
map_int() |
map2_int() |
integer vector |
map_lgl() |
map2_lgl() |
logical vector |
map_dfc() |
map2_dfc() |
data frame (by column) |
map_dfr() |
map2_dfr() |
data frame (by row) |
i
i
input 1 | input 2 | input n | returns |
---|---|---|---|
map() |
map2() |
pmap() |
list |
map_chr() |
map2_chr() |
pmap_chr() |
character vector |
map_dbl() |
map2_dbl() |
pmap_dbl() |
double vector (numeric) |
map_int() |
map2_int() |
pmap_int() |
integer vector |
map_lgl() |
map2_lgl() |
pmap_lgl() |
logical vector |
map_dfc() |
map2_dfc() |
pmap_dfc() |
data frame (by column) |
map_dfr() |
map2_dfr() |
pmap_dfr() |
data frame (by row) |
walk() |
walk2() |
pwalk() |
input (side effects!) |
base R | purrr |
---|---|
lapply() |
map() |
vapply() |
map_*() |
sapply() |
? |
x[] <- lapply() |
map_dfc() |
mapply() |
map2() , pmap() |
x <- rnorm(10)y <- map(x, mean)
x <- rnorm(10)y <- vector("list", length(x))for (i in seq_along(x)) { y[[i]] <- mean(x[[i]])}
x <- rnorm(10)y <- map(x, mean)
x <- rnorm(10) y <- vector("list", length(x))for (i in seq_along(x)) { y[[i]] <- mean(x[[i]]) }
x <- rnorm(10)y <- map(x, mean)
x <- rnorm(10)y <- vector("list", length(x)) for (i in seq_along(x)) { y[[i]] <- mean(x[[i]]) }
x <- rnorm(10)y <- map(x, mean)
x <- rnorm(10)y <- vector("list", length(x)) for (i in seq_along(x)) { y[[i]] <- mean(x[[i]])}
Slides created via the R package xaringan.
exercises.Rmd
in the repo: github.com/malcolmbarrett/lawrug_purrrKeyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |