knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
Experimenting with yet another way to do rowwise operations.
You can install rap
from gitub
# install.packages("devtools") devtools::install_github("romainfrancois/rap")
This offers rap()
as an alternative to some versions of:
rowwise()
+ do()
mutate()
+ pmap()
purrrlyr
?rap()
works with lambdas supplied as formulas, similar to purrr::map()
but instead of .x
, .y
, ..1
, ..2
, ...the lambda can use the column names,
which stand for a single element of the associated vector, in the [[
sense.
library(tidyverse) library(rap) tbl <- tibble(cyl_threshold = c(4, 6, 8), mpg_threshold = c(30, 25, 20)) tbl tbl %>% rap(x = ~filter(mtcars, cyl == cyl_threshold, mpg < mpg_threshold))
If the lhs of the formula is empty, rap()
adds a list column. Otherwise the lhs
can be used to specify the type:
tbl %>% rap( x = ~ filter(mtcars, cyl == cyl_threshold, mpg < mpg_threshold), n = integer() ~ nrow(x) )
this example is based on this issue,
which has equivalent with pmap
:
tbl %>% mutate( x = pmap( .l = list(cyl_threshold, mpg_threshold), function(cc, mm) filter(mtcars, cyl == cc, mpg < mm) ), n = map_int(x, nrow) )
library(dplyr) starwars <- head(starwars) # creates a list of length 1 integer vectors # because type not specified starwars %>% wap(~length(films)) # using the lhs to specify the type starwars %>% wap(integer() ~ length(films)) # list of data frames starwars %>% wap(~ data.frame(vehicles = length(vehicles), starships = length(starships))) # Specify type as data.frame() row binds them starwars %>% wap(data.frame() ~ data.frame(vehicles = length(vehicles), starships = length(starships)))
r emo::ji("lemon")
zest_join()
is similar to dplyr::nest_join()
but
you control what goes in the nested column. Z
is N
but r emo::ji("arrow_heading_down")
.
tbl <- tibble(cyl_threshold = c(4, 6, 8), mpg_threshold = c(30, 25, 20)) tbl %>% zest_join(mtcars, data = ~cyl == cyl_threshold & mpg < mpg_threshold)
In the rhs of the formula :
cyl
and mpg
refer to columns of mtcars
cyl_threshold
and mpg_threshold
refer to the current value from tbl
because these columns don't exist in mtcars. If you wanted to refer to columns that are present both in mtcars and tbl you would have to unquote the columns in tbl with the unquoting operator, e.g. !!cylAdd the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.