(4) List Columns
— LAST YEAR’S CONTENT BELOW —
26.11 Today’s Agenda
Today’s lessons are:
- Review of
purrr
and piping. - Parallel mapping
- List columns
- An analysis using both
26.12 Resources
All are from Jenny’s purrr
tutorial. Specifically:
- Parallel mapping: Jenny’s “Specifying the function in map() + parallel mapping”
- List columns in data frames; nesting: Jenny’s “List Columns”.
The all-encompassing application near the bottom of the worksheet is from Jenny’s “Sample from groups, n varies by group”
26.13 Review
26.13.1 purrr
map(x, f, ...)
returns a list with elements:
f(x[[1]], ...)
f(x[[2]], ...)
- …
map_dbl
, map_lgl
, etc to return a vector.
We can specify a pre-defined f
, or write it on-the-fly, or another way that we didn’t touch on last time. Example with “squaring” function:
map(x, square)
wheresquare <- function(t) t^2
;map(x, function(t) t^2)
; ormap(x, ~ (.x)^2)
(function variable is.x
by convention).
26.13.2 piping: .
We know that a %>% b()
is the same as b(a)
.
Want to refer to a
in addition to the first argument? Specify it as a .
. Example:
Gotcha:
Case 1: LHS (= left-hand side) not put as first argument when .
appears in RHS:
## [1] 3
## [1] 3
Case 2: LHS is still put as first argument, even when .
appears in RHS:
## [1] 11 32
## [1] 1 2 3 4 5 6 7 8 9 10 1 10
Trick: Use {}
to “absorb” the placement of LHS as first argument:
## [1] 1 10