knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

tiedr

Lifecycle: experimental Travis build status

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.

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()

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()

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()

# 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)


yutannihilation/tiedr documentation built on May 28, 2019, 9:54 p.m.