An open API service indexing awesome lists of open source software.

https://github.com/UchidaMizuki/dibble

Dimensional Data Frames
https://github.com/UchidaMizuki/dibble

multidimensional-arrays r tidy-data

Last synced: 4 months ago
JSON representation

Dimensional Data Frames

Awesome Lists containing this project

README

        

---
output: github_document
---

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

# dibble

[![CRAN status](https://www.r-pkg.org/badges/version/dibble)](https://CRAN.R-project.org/package=dibble)
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[![R-CMD-check](https://github.com/UchidaMizuki/dibble/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/UchidaMizuki/dibble/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/UchidaMizuki/dibble/branch/main/graph/badge.svg)](https://app.codecov.io/gh/UchidaMizuki/dibble?branch=main)

A 'dibble' (derived from 'dimensional tibble') is a data frame consisting of arrays with dimension names, known as data cubes.
The columns of the dibbles are classified into dimensions or measures, and the operations on the measures are broadcasted by dimension names.

## Installation

``` r
# the released version from CRAN:
install.packages("dibble")

# the development version from GitHub:
# install.packages("devtools")
devtools::install_github("UchidaMizuki/dibble")
```

## Examples

```{r,message=FALSE,warning=FALSE}
library(dibble)
library(dplyr)
library(tidyr)
```

### Broadcasting
```{r}
arr1 <- array(1:6, c(2, 3),
list(axis1 = letters[1:2],
axis2 = letters[1:3]))
arr2 <- array(1:2, 2,
list(axis2 = letters[1:2]))

try(arr1 * arr2)

ddf1 <- as_dibble(arr1)
ddf2 <- as_dibble(arr2)

ddf1 * ddf2
# You can use broadcast() to suppress the warnings.
broadcast(ddf1 * ddf2,
dim_names = c("axis1", "axis2"))
```

### dplyr methods

dibble provides some dplyr methods as follows,

- `as_tibble()`: From tibble package
- `filter()`
- `mutate()`: Experimental
- `rename()`
- `select()` and `relocate()`
- `slice()`: Specify locations (a integer vector) for each dimension

### How to build a dibble

#### From a data.frame

```{r}
df <- expand_grid(axis1 = letters[1:2],
axis2 = letters[1:2]) |>
mutate(value1 = row_number(),
value2 = value1 * 2)

ddf <- df |>
dibble_by(axis1, axis2)
ddf
# You can access the measures from the dibble with `$`.
ddf$value1

df <- expand_grid(tibble(axis1_key = letters[1:2],
axis1_value = 1:2),
tibble(axis2_key = letters[1:2],
axis2_value = 1:2)) |>
mutate(value1 = row_number(),
value2 = value1 * 2)

# You can `pack` several columns into one dimension (See `tidyr::pack()`).
df |>
dibble_by(axis1 = c(axis1_key, axis1_value),
axis2 = c(axis2_key, axis2_value),
.names_sep = "_")
```

#### From an array with dimension names or a vector

dibble provides some dplyr methods as follows,

```{r}
# from an array with dimension names
arr <- array(1:4, c(2, 2),
list(axis1 = letters[1:2],
axis2 = letters[1:2]))

ddf1 <- as_dibble(arr)

# from a vector
ddf2 <- broadcast(1:4,
list(axis1 = letters[1:2],
axis2 = letters[1:2]))

arr
ddf1
ddf2
```