knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
The goal of {indexvctrs} is to provide a DSL for indexed vector operations
Pretty experimental right now!
devtools::install_github("jameelalsalam/indexvctrs")
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)
I hope that this package benefits from learning from some of these:
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.