knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
The groupr package is designed to work with tibbles and dplyr. It provides replacements for tidyverse grouping and pivoting operations, and uses richer data structures to make these operations easier to think about.
There are two main ideas behind this package. First is the idea of inapplicable data. While we often use NA
as a placeholder for unknown but important information, R doesn't provide a way to mark data that should definitely be ignored. groupr
provides an inapplicable value (printed <I>
).
The second main idea is that pivoting is just a way to rearrange groups of data. Some kinds of pivots cannot be expressed by a single tidyr pivot statement and require two or even three consecutive pivot calls. Inapplicable groups can be used to describe some of these more complex operations in a very straightforward way.
devtools::install_github("ngriffiths21/groupr")
library(groupr) library(dplyr, warn.conflicts = FALSE) library(tidyr)
Make columns out of row groups:
p_df2 <- tibble::tribble( ~ grp1, ~ grp2, ~ val, "A", 1, 1.9, "A", 2, 10.1, "B", 2, 3.1, "B", 1, 4.7, "C", NA, 4.9 )
p_df2 # group and make the NA an inapplicable grouping p_df2 <- group_by2(p_df2, grp1, grp2 = NA) group_data(p_df2) # groups version of pivot pivot_grps(p_df2, cols = "grp1") # tidyr version pivot_wider(p_df2, names_from = grp1, values_from = val)
Note that with this inapplicable grouping, the value from the "C" group is applied to both subgroups. This behavior is not possible using tidyr.
Make row groups out of columns (pivot longer):
p_df <- group_by2(iris, Species) # groups version: make column grouping, then pivot colgrouped <- sep_colgrp(p_df, ".", index_name = "Measurement") colgrouped pivot_grps(colgrouped, rows = "Measurement") # tidyr version pivot_longer(iris, cols = c(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width), values_to = "value")
Using this approach we can preserve separate columns for each flower part rather than combining them into one.
Pivot both rows and columns:
p_df3 <- pivot_grps(p_df2, cols = "grp1") p_df3 # groups version pivot_grps(p_df3, rows = "grp1", cols = "grp2")
At this point this is experimental, with limited testing. The API is likely to change.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.