-
Notifications
You must be signed in to change notification settings - Fork 7
/
README.Rmd
executable file
·125 lines (91 loc) · 3.18 KB
/
README.Rmd
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# rap <img src="man/figures/logo.png" align="right" />
[![Lifecycle Status](https://img.shields.io/badge/lifecycle-experimental-blue.svg)](https://www.tidyverse.org/lifecycle/)
[![Travis build status](https://travis-ci.org/romainfrancois/rap.svg?branch=master)](https://travis-ci.org/romainfrancois/rap)
![](https://media.giphy.com/media/l41Yy7rv1mVZNQCT6/giphy.gif)
Experimenting with yet another way to do rowwise operations.
## Installation
You can install `rap` from gitub
``` r
# install.packages("devtools")
devtools::install_github("romainfrancois/rap")
```
## Why
This offers `rap()` as an alternative to some versions of:
- `rowwise()` + `do()`
- `mutate()` + `pmap()`
- maybe `purrrlyr` ?
- probably other approaches
`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.
## rap
```{r}
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:
```{r}
tbl %>%
rap(
x = ~ filter(mtcars, cyl == cyl_threshold, mpg < mpg_threshold),
n = integer() ~ nrow(x)
)
```
this example is based on this [issue](https://github.com/tidyverse/purrr/issues/280),
which has equivalent with `pmap`:
```{r}
tbl %>%
mutate(
x = pmap(
.l = list(cyl_threshold, mpg_threshold),
function(cc, mm) filter(mtcars, cyl == cc, mpg < mm)
),
n = map_int(x, nrow)
)
```
## wap
```{r}
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)))
```
## zest_join
`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")`.
```{r}
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. !!cyl