MyNixOS website logo
Description

Obtaining a Flat Table from Pivot Tables.

Transformations that allow obtaining a flat table from reports in text or Excel format that contain data in the form of pivot tables. They can be defined for a single report and applied to a set of reports.

flattabler flattabler website

CRANstatus R-CMD-check Codecov testcoverage Downloads

Pivot tables are generally used to present raw and summary data. They are generated from spreadsheets and, more recently, also from R (pivottabler).

If we generate pivot tables from our own data, flattabler package is not necessary. But, if we get data in pivot table format and need to represent or analyse it using another tool, this package can be very helpful: It can save us several hours of programming or manual transformation.

flattabler package offers a set of operations that allow us to transform one or more pivot tables into a flat table.

Installation

You can install the released version of flattabler from CRAN with:

install.packages("flattabler")

And the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("josesamos/flattabler")

Example

A pivot table contains label rows and columns, and an array of values, usually numeric data. It can contain additional information, such as table header or footer.

Below is an example of a pivot table obtained from the pivottabler package. It is included in flattabler package in the form of the variable df_pivottabler, defined as a data frame.

V1V2V3V4V5V6V7V8V9V10
Express PassengerOrdinary PassengerTotal
DMUEMUHSTTotalDMUEMUTotal
Number of TrainsArriva Trains Wales307930798308303909
CrossCountry2213373222865636322928
London Midland56388849144875591282013379248279
Virgin Trains2137645785948594
Total3298715306732490256484282013468583710
Maximum SpeedArriva Trains Wales9090909090
CrossCountry125125125100100125
London Midland100110110100100100110
Virgin Trains125125125125
Total125125125125100100100125

The transformation to obtain a flat table from the previous pivot table using flattabler package is as follows:

library(flattabler)

ft <- pivot_table(df_pivottabler) |>
  define_labels(n_col = 2, n_row = 2) |>
  fill_labels() |>
  remove_agg() |>
  fill_values() |>
  unpivot(na_rm = TRUE)

The result is a tibble object that can be further transformed, for example, by the dplyr package to remove the added data.

ft <- ft |>
  dplyr::filter(col2 != "Total") |>
  dplyr::filter(row2 != "Total")

The result obtained is as follows:

col1col2row1row2value
Number of TrainsArriva Trains WalesExpress PassengerDMU3079
Number of TrainsArriva Trains WalesOrdinary PassengerDMU830
Number of TrainsCrossCountryExpress PassengerDMU22133
Number of TrainsCrossCountryExpress PassengerHST732
Number of TrainsCrossCountryOrdinary PassengerDMU63
Number of TrainsLondon MidlandExpress PassengerDMU5638
Number of TrainsLondon MidlandExpress PassengerEMU8849
Number of TrainsLondon MidlandOrdinary PassengerDMU5591
Number of TrainsLondon MidlandOrdinary PassengerEMU28201
Number of TrainsVirgin TrainsExpress PassengerDMU2137
Number of TrainsVirgin TrainsExpress PassengerEMU6457
Maximum SpeedArriva Trains WalesExpress PassengerDMU90
Maximum SpeedArriva Trains WalesOrdinary PassengerDMU90
Maximum SpeedCrossCountryExpress PassengerDMU125
Maximum SpeedCrossCountryExpress PassengerHST125
Maximum SpeedCrossCountryOrdinary PassengerDMU100
Maximum SpeedLondon MidlandExpress PassengerDMU100
Maximum SpeedLondon MidlandExpress PassengerEMU110
Maximum SpeedLondon MidlandOrdinary PassengerDMU100
Maximum SpeedLondon MidlandOrdinary PassengerEMU100
Maximum SpeedVirgin TrainsExpress PassengerDMU125
Maximum SpeedVirgin TrainsExpress PassengerEMU125

Once we have defined the necessary transformations for a pivot table, we can apply them to any other with the same structure. Candidate tables can have different number of rows or columns, depending on the number of labels, but they must have the same number of rows and columns of labels, and the same number of header or footer rows, so that the transformations are the same for each table.

To easily perform this operation, we define a function f from the transformations, as shown below.

f <- function(pt) {
  pt |>
    set_page(1, 1) |>
    define_labels(n_col = 2, n_row = 2) |>
    remove_top(1) |>
    fill_labels() |>
    remove_agg() |>
    fill_values() |>
    remove_k() |>
    replace_dec() |>
    unpivot()
}

folder <- system.file("extdata", "csvfolder", package = "flattabler")
lpt <- read_text_folder(folder)

lft <- flatten_table_list(lpt, f)

lft
#> # A tibble: 201 × 6
#>    page  col1  col2  row1  row2  value
#>    <chr> <chr> <chr> <chr> <chr> <chr>
#>  1 M1    b1    a01   e2    d4    1.88 
#>  2 M1    b1    a05   e1    d1    1.91 
#>  3 M1    b1    a05   e2    d3    1.10 
#>  4 M1    b1    a05   e2    d4    2.25 
#>  5 M1    b1    a09   e1    d1    2.55 
#>  6 M1    b1    a09   e1    d2    2.74 
#>  7 M1    b1    a09   e2    d3    3.99 
#>  8 M1    b1    a13   e1    d1    2.99 
#>  9 M1    b1    a13   e1    d2    1.02 
#> 10 M1    b1    a13   e2    d3    3.48 
#> # ℹ 191 more rows

In this way we can generate a flat table from a list of pivot tables. The list of pivot tables is generated using package functions to import them from various data sources.

Metadata

Version

2.1.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