Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/yutannihilation/tiedr

Experimental package (not yet existent) for multi-gather() and multi-spread()
https://github.com/yutannihilation/tiedr

Last synced: about 1 month ago
JSON representation

Experimental package (not yet existent) for multi-gather() and multi-spread()

Awesome Lists containing this project

README

        

---
output: github_document
editor_options:
chunk_output_type: console
---

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

[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)
[![Travis build status](https://travis-ci.com/yutannihilation/tiedr.svg?branch=master)](https://travis-ci.com/yutannihilation/tiedr)

tiedr is an experimental package to "pack" (or "bundle") some columns to a data.frame column.

The idea behind this package is basically the same as `pack()`/`unpack()`, which was proposed on the tidyr's issue below. I didn't notice this issue at the time of deciding the main function name as `bundle()`...

*

If you wonder how this is useful, my blog post might help:

*

## Usages

### `bundle()` and `unbundle()`

`bundle()` packs columns into a data.frame column, and `unbundle()` unpacks it.

```{r}
library(tiedr)

iris <- tibble::as_tibble(iris)

# bundle() takes unnamed arguments
iris %>%
bundle(-Species)

# bundle() also takes named arguments
d <- iris %>%
bundle(Sepal = starts_with("Sepal"),
Petal = starts_with("Petal"))

# bundled columns can be bundled
d %>%
bundle(-Species)

# unbundle()
unbundle(d, Sepal, Petal, sep = NULL)
```

### `auto_bundle()`

```{r}
d <- tibble::tribble(
~A_a_x, ~A_a_y, ~A_b_x, ~B_a_x, ~B_b_x, ~B_b_y,
1, 2, 3, 4, 5, 6,
2, 3, 4, 5, 6, 7
)

auto_bundle(d, everything())
auto_bundle(d, starts_with("A"))
```

### `gather_bundles()`

```{r}
auto_bundle(d, everything()) %>%
gather_bundles()

auto_bundle(d, everything()) %>%
gather_bundles(.key = "key_outer") %>%
gather_bundles(.key = "key_inner")

auto_bundle(d, everything()) %>%
gather_bundles(.key = "key_outer") %>%
gather_bundles(.key = "key_inner") %>%
gather_bundles(x:y, .key = "xy")
```

### `spread_bundles()`

```{r}
# https://github.com/tidyverse/tidyr/issues/149
d <- tibble::tribble(
~hw, ~name, ~mark, ~pr,
"hw1", "anna", 95, "ok",
"hw1", "alan", 90, "meh",
"hw1", "carl", 85, "ok",
"hw2", "alan", 70, "meh",
"hw2", "carl", 80, "ok"
)

spread_bundles(d, name, c(mark, pr))

spread_bundles(d, name, c(mark, pr)) %>%
unbundle(-hw)

# without prefix
spread_bundles(d, name, c(mark, pr)) %>%
unbundle(-hw, sep = NULL)

# ?spread
tibble::tibble(x = c("a", "b"), y = c(3, 4), z = c(5, 6)) %>%
spread_bundles(x, y)
```