(4) List Columns

— LAST YEAR’S CONTENT BELOW —

26.11 Today’s Agenda

Today’s lessons are:

  1. Review of purrr and piping.
  2. Parallel mapping
  3. List columns
  4. An analysis using both

26.12 Resources

All are from Jenny’s purrr tutorial. Specifically:

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) where square <- function(t) t^2;
  • map(x, function(t) t^2); or
  • map(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

26.13.3 Worksheet

To get participation points for today, we’ll be filling out the cm104-exercise.Rmd file.

Add this to your participation repo.