MyNixOS website logo
Description

Forest/Tree Data Frames.

Provides data frames for forest or tree data structures. You can create forest data structures from data frames and process them based on their hierarchies.

timbr

CRANstatus Lifecycle:experimental Codecov testcoverage R-CMD-check

timbr provides data frames for forest or tree data structures. You can create forest data structures from data frames and process them based on their hierarchies.

Installation

You can install the development version of timbr from GitHub with:

# the released version from CRAN:
install.packages("timbr")

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

Main Functions

The main functions provided by timbr are as follows,

  • children()
  • climb()
  • leaves()
  • traverse()
  • rbind()

tidyverse methods

timbr provides some tidyverse methods as follows,

  • mutate()
  • summarise()
  • select() and relocate()
  • rows_update() and rows_patch()

Examples

library(timbr)
library(dplyr)
fr <- tidyr::expand_grid(key1 = letters[1:2],
                         key2 = letters[1:2],
                         key3 = letters[1:2]) |>  
  mutate(value = row_number()) |> 
  forest_by(key1, key2, key3)

fr_sum <- fr |> 
  summarise(value = sum(value)) |> 
  summarise(value = sum(value))

fr
#> # A forest: 8 nodes and 1 feature
#> # Groups:   key1, key2 [4]
#> # Trees:
#> #   key3 [8]
#>   key1  key2  .        value
#>   <chr> <chr> <node>   <int>
#> 1 a     a     <key3> a     1
#> 2 a     a     <key3> b     2
#> 3 a     b     <key3> a     3
#> 4 a     b     <key3> b     4
#> 5 b     a     <key3> a     5
#> 6 b     a     <key3> b     6
#> 7 b     b     <key3> a     7
#> 8 b     b     <key3> b     8
fr_sum
#> # A forest: 14 nodes and 1 feature
#> # Trees:
#> #   key1 [2]
#> #   └─key2 [4]
#> #     └─key3 [8]
#>   .        value
#>   <node>   <int>
#> 1 <key1> a    10
#> 2 <key1> b    26
children(fr_sum)
#> # A forest: 12 nodes and 1 feature
#> # Groups:   key1 [2]
#> # Trees:
#> #   key2 [4]
#> #   └─key3 [8]
#>   key1  .        value
#>   <chr> <node>   <int>
#> 1 a     <key2> a     3
#> 2 a     <key2> b     7
#> 3 b     <key2> a    11
#> 4 b     <key2> b    15
fr_sum |> 
  climb(key3)
#> # A forest: 8 nodes and 1 feature
#> # Trees:
#> #   key3 [8]
#>   .        value
#>   <node>   <int>
#> 1 <key3> a     1
#> 2 <key3> b     2
#> 3 <key3> a     3
#> 4 <key3> b     4
#> 5 <key3> a     5
#> 6 <key3> b     6
#> 7 <key3> a     7
#> 8 <key3> b     8
fr1 <- tidyr::expand_grid(key1 = letters[1:2], 
                          key2_1 = letters[1:2],
                          key3_1 = letters[1:2]) |> 
  mutate(value = row_number()) |> 
  forest_by(key1, key2_1, key3_1) |> 
  summarise(value = sum(value))

fr2 <- tidyr::expand_grid(key1 = letters[1:2], 
                          key2_2 = letters[1:2],
                          key3_2 = letters[1:2]) |> 
  mutate(value = row_number()) |> 
  forest_by(key1, key2_2, key3_2) |> 
  summarise(value = sum(value))

fr <- rbind(fr1, fr2)
fr_sum <- fr |> 
  summarise(value = sum(value))

fr
#> # A forest: 24 nodes and 1 feature
#> # Groups:   key1 [2]
#> # Trees:
#> #   key2_1 [4]
#> #   └─key3_1 [8]
#> #   key2_2 [4]
#> #   └─key3_2 [8]
#>   key1  .          value
#>   <chr> <node>     <int>
#> 1 a     <key2_1> a     3
#> 2 a     <key2_1> b     7
#> 3 b     <key2_1> a    11
#> 4 b     <key2_1> b    15
#> 5 a     <key2_2> a     3
#> 6 a     <key2_2> b     7
#> 7 b     <key2_2> a    11
#> 8 b     <key2_2> b    15
fr_sum
#> # A forest: 26 nodes and 1 feature
#> # Trees:
#> #   key1 [2]
#> #   ├─key2_1 [4]
#> #   │ └─key3_1 [8]
#> #   └─key2_2 [4]
#> #     └─key3_2 [8]
#>   .        value
#>   <node>   <int>
#> 1 <key1> a    20
#> 2 <key1> b    52
traverse(fr_sum,
         function(x, children) {
           x$value <- prod(children$value)
           x
         })
#> # A forest: 26 nodes and 1 feature
#> # Trees:
#> #   key1 [2]
#> #   ├─key2_1 [4]
#> #   │ └─key3_1 [8]
#> #   └─key2_2 [4]
#> #     └─key3_2 [8]
#>   .          value
#>   <node>     <int>
#> 1 <key1> a     576
#> 2 <key1> b 2822400
Metadata

Version

0.2.2

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows