---

output: github_document

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

indexvctrs

R build status R-CMD-check

The goal of {indexvctrs} is to provide a DSL for indexed vector operations

Pretty experimental right now!

Installation

devtools::install_github("jameelalsalam/indexvctrs")

Example

This package provides idx_tbl objects which behave somewhat like sparse multi-dimensional arrays with named indices, but remain data frames as well. One of the columns is the value column, which is manipulated directly through math operations.

When idx_tbl objects are created, columns are marked as index columns (the idx_cols attribute), and a single value column is stored as value. Additional columns are dropped.

library(indexvctrs)

crop_acres <- idx_tibble(
  tibble::tribble(
    ~crop,   ~year, ~value,
    "corn",  2005,  4,
    "wheat", 2005,  5,
    "corn",  2010,  6,
    "wheat", 2010,  8
  ),

  idx_cols = c("crop", "year")
)

EF <- idx_tibble(
  tibble::tribble(
    ~crop, ~value,
    "corn", 1.5,
    "wheat", 2
  ), idx_cols = "crop"
)

idx_cols(crop_acres)
crop_acres

Math operations can be performed between idx_tbls with common indices via join semantics. The result of this is that where indices do not share an axis, values are broadcast (or recycled) across that axis. This applies, for example, to scalars.

EF * crop_acres
EF * 2

It can be convenient to express relationships using functions (e.g., so that they can be stated out-of-order):

calc_emissions <- function(activity, EF) {activity * EF}

emissions_by_crop <- calc_emissions(activity = crop_acres, EF = EF * 2)
emissions_by_crop

Vector operations act on the value column:

sum(emissions_by_crop)

If something isn't implemented, never fear because you should be able to drop back to data frame operations.

library(tidyverse)

emissions_by_crop %>%
  filter(year == 2010)

References

I hope that this package benefits from learning from some of these:



jameelalsalam/indexvctrs documentation built on Oct. 28, 2023, 3:26 a.m.