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

nestedcats

This package aims to make it possible to store and manipulate rich hierarchical category information and do split-apply-combine operations on these categories.

Installation

You can install the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("jameelalsalam/nestedcats")

Example

This is a basic example which shows you how to solve a common problem:

Suppose you have information about a tree of nested categories. This package takes the approach of representing this as a tbl_graph from the tidygraph package.

suppressPackageStartupMessages(library(tidyverse))
suppressPackageStartupMessages(library(tidygraph))
suppressPackageStartupMessages(library(ggraph))

library(nestedcats)

cats <- tibble(name = c("1", "1a", "1a1", "1a2", "1b"))

cat_tree <- tibble::tibble(
  parent = c("1", "1", "1a", "1a"),
  child =  c("1a", "1b", "1a1", "1a2")
)

cat_g <- tbl_graph(cats, cat_tree)

viz_cats_ggraph_tree(cat_g)

And further suppose you have information about the leaf nodes.

leaf_data <- tibble(
  cat = c("1b", "1a1", "1a2"),
  value = c(1, 2, 3)
)

cat_tree <- tibble::tibble( parent = c(NA_character, "1", "1", "1a", "1a"), child = c("1", "1a", "1b", "1a1", "1a2") ) %>% as_closure_tbl()

leaf_data %>% group_by_cats(cat = c("1a", "1b"), along = cat_tree) %>% summarize(value = sum(value, na.rm = TRUE))

verify_covers(c("1a", "1b"), over = cat_tree) verify_disjoint(c("1a", "1b"), along = cat_tree) verify_disjoint_covers(c("1a", "1b"), along = cat_tree)

all.equal(coverage(c("1a", "1b"), along = cat_tree), coverage("1", along = cat_tree))



jameelalsalam/nestedcats documentation built on June 2, 2020, 8:16 p.m.