-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.Rmd
127 lines (98 loc) · 3.27 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# dibble
<!-- badges: start -->
[![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)
<!-- badges: end -->
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
```