Skip to content

Commit

Permalink
Add/rename/link pmap example
Browse files Browse the repository at this point in the history
  • Loading branch information
jennybc committed Apr 11, 2018
1 parent 66cd40f commit 23ab029
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 278 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Not all are used in webinar
* **Working with non-vectorized functions.** [`ex04_map-example`](ex04_map-example.md) Small example using `purrr::map()` to apply `nrow()` to list of data frames.
* **Row-wise thinking vs. column-wise thinking.** [`ex05_attack-via-rows-or-columns`](ex05_attack-via-rows-or-columns.md) Data rectangling example. Both are possible, but I find building a tibble column-by-column is less aggravating than building rows, then row binding.
* **Iterate over rows of a data frame.** [`iterate-over-rows`](iterate-over-rows.md) Empirical study of reshaping a data frame into this form: a list with one component per row. Revisiting a study originally done by Winston Chang. Run times for different number of [rows](row-benchmark.png) or [columns](col-benchmark.png).
* **Generate data from different distributions via `purrr::pmap()`.** [`ex06_runif-via-pmap`](ex06_runif-via-pmap.md) Use `purrr::pmap()` to generate U[min, max] data for various combinations of (n, min, max), stored as rows of a data frame.
* **Split-apply-combine.** Nesting vs splitting.
- Downside of `split()`: First-class grouping variable(s) --> character vector of names --> variable is a big drag. Integer-y numerics must be coerced back, factors must be recreated, with original levels. Transitting data through attributes is an anti-pattern.
- Downside of `nest()`: When you inspect the list-column, you can't see values of grouping (key) variables. Grouping variables not necessarily/easily available for simple map (coolbutuseless's posts and PR).
278 changes: 0 additions & 278 deletions ex05_nesting-is-good.md

This file was deleted.

7 changes: 7 additions & 0 deletions ex07_runif-row-wise.R → ex06_runif-via-pmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ my_runif <- function(n, min, max, ...) runif(n, min, max)
set.seed(123)
pmap(df_oops, my_runif)

#' ## Add the generated data to the data frame as a list-column
set.seed(123)
(df_aug <- df %>%
mutate(data = pmap(., runif)))
#View(df_aug)

#' ## Review
#'
#' What have we done?
Expand All @@ -148,3 +154,4 @@ pmap(df_oops, my_runif)
#' * Wrote custom wrappers around `runif()` to deal with:
#' - df var names != `.f()` arg names
#' - df vars that aren't formal args of `.f()`
#' * Added generated data as a list-column
16 changes: 16 additions & 0 deletions ex07_runif-row-wise.md → ex06_runif-via-pmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,21 @@ pmap(df_oops, my_runif)
#> [1] 894.7157 946.4206 141.0008
```

## Add the generated data to the data frame as a list-column

``` r
set.seed(123)
(df_aug <- df %>%
mutate(data = pmap(., runif)))
#> # A tibble: 3 x 4
#> n min max data
#> <int> <dbl> <dbl> <list>
#> 1 1 0. 1. <dbl [1]>
#> 2 2 10. 100. <dbl [2]>
#> 3 3 100. 1000. <dbl [3]>
#View(df_aug)
```

## Review

What have we done?
Expand All @@ -263,3 +278,4 @@ What have we done?
- Wrote custom wrappers around `runif()` to deal with:
- df var names \!= `.f()` arg names
- df vars that aren’t formal args of `.f()`
- Added generated data as a list-column
File renamed without changes.

0 comments on commit 23ab029

Please sign in to comment.