Description Usage Arguments Value Examples
Map over columns of a data frame simultaneously
1 2 3 4 5 |
.tbl |
A data frame |
.f |
a single formula |
... |
formulas The rhs of each formula uses columns of The lhs of each formula indicates the type, in the
In |
wap()
returns a vector of the type specified by the lhs of the formula.
The vector validates vec_size() == nrow(.tbl)
. This is similar
to purrr::pmap()
rap()
adds a column to .tbl
per formula in ...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | library(purrr)
library(dplyr)
library(tibble)
tbl <- tibble(cyl_threshold = c(4, 6, 8), mpg_threshold = c(30, 25, 20))
# ----- wap
# returns a list of 3 elements
tbl %>%
wap( ~ filter(mtcars, cyl == cyl_threshold, mpg < mpg_threshold))
# same, i.e. list() is equivalent to empty
tbl %>%
wap(list() ~ filter(mtcars, cyl == cyl_threshold, mpg < mpg_threshold))
# can specify the output type with the formula lhs
tbl %>%
wap(integer() ~ nrow(filter(mtcars, cyl == cyl_threshold, mpg < mpg_threshold)))
# to make data frames
starwars %>%
wap(data.frame() ~ data.frame(species = length(species), films = length(films)))
# ----- rap: add columns
tbl %>%
rap(
x = ~ filter(mtcars, cyl == cyl_threshold, mpg < mpg_threshold),
n = integer() ~ nrow(x)
)
# rap is especially useful for iterating over multiple models
starwars %>%
group_nest(gender) %>%
rap(
model = ~ lm(height ~ mass + birth_year, data = data),
perf = double() ~ summary(model)$adj.r.squared
)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.