Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/romainfrancois/rap

yet another experimental way of processing a data.frame rowwisely
https://github.com/romainfrancois/rap

Last synced: about 1 month ago
JSON representation

yet another experimental way of processing a data.frame rowwisely

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

# rap

[![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