Learn R Programming

⚠️There's a newer version (2.1.2) of this package.Take me there.

flattabler

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 development version with:

# install.packages("flattabler")
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.

V1V2V3V4V5V6V7V8V9
M1ED
e1Total e1e2Total e2Total general
ABd1d2d1d2
a1b12,991,024,014,061,325,389,39
b23,893,657,545,555,5513,09
b32,332,331,871,874,2
Total a19,214,6713,8811,481,3212,826,68
a2b15,621,947,564,592,136,7214,28
b23,827,7211,544,782,947,7219,26
b35,366,3811,741,691,783,4715,21
Total a214,816,0430,8411,066,8517,9148,75
Total general24,0120,7144,7222,548,1730,7175,43

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

library(flattabler)
library(tidyr)

ft <- 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()

The result obtained is as follows:

pagecol1col2row1row2value
M1a1b1e1d12.99
M1a1b1e1d21.02
M1a1b1e2d14.06
M1a1b1e2d21.32
M1a1b2e1d13.89
M1a1b2e1d23.65
M1a1b2e2d15.55
M1a1b3e1d12.33
M1a1b3e2d11.87
M1a2b1e1d15.62
M1a2b1e1d21.94
M1a2b1e2d14.59
M1a2b1e2d22.13
M1a2b2e1d13.82
M1a2b2e1d27.72
M1a2b2e2d14.78
M1a2b2e2d22.94
M1a2b3e1d15.36
M1a2b3e1d26.38
M1a2b3e2d11.69
M1a2b3e2d21.78

The table above is a flat table whose data has been obtained from the pivot table through flattabler. It only contains raw data and the labels that characterize it. An additional label has been added with the value that identifies the pivot table, the pivot table page. NA values have not been included.

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()
}

ft <- flatten_table_list(list_pt_ie, f)

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.

Copy Link

Version

Install

install.packages('flattabler')

Monthly Downloads

269

Version

1.0.0

License

MIT + file LICENSE

Maintainer

Jose Samos

Last Published

June 29th, 2020

Functions in flattabler (1.0.0)

list_pt

List of pivot tables
remove_agg

Remove rows and columns with aggregated data
remove_bottom

Remove bottom rows from a pivot table
list_pt_compact

Pivot tables with a column with data from two label fields
pivot_table

pivot_table S3 class
remove_cols

Remove columns from a pivot table
remove_empty

Remove empty rows and columns from a pivot table
pt_ine2871

Official population in the municipalities of Granada (Spain)
new_pivot_table

pivot_table S3 class
fill_labels

Fill in missing labels
pt_pivottabler

Pivot table with basic and subtotal labels in the same column
list_pt_ie

List of pivot tables used in the vignette
spacer_rows

Spacer rows
remove_top

Remove top rows from a pivot table
replace_dec

Replace decimal separator
remove_rows

Remove rows from a pivot table
pt_set_h_v

Set of pivot tables on one sheet
remove_right

Remove right columns from a pivot table
remove_k

Remove thousands separator
remove_left

Remove left columns from a pivot table
pt_set_h

Set of pivot tables placed horizontally on one sheet
pt_m4

Pivot table with with thousands indicator and decimal numbers
spacer_columns

Spacer columns
set_page

Set page information to a pivot_table object
pt_set_v

Set of pivot tables placed vertically on one sheet
pt_m4_compact

Pivot table with a column with data from two label fields
unpivot

Unpivot a pivot table
read_text_folder

Import all text files in a folder
read_text_file

Import text file
view_table_attr

View table and attributes
define_labels

Define the quantity of rows and columns that contain labels
get_col_values

Get column values
fill_values

Fill in missing values
fill_vector

Fill in missing values in a vector
get_page

Get the page information of a pivot_table object
divide

Divide table
flatten_table_list

Transform a pivot table list into a flat table
flattabler

Obtaining a Flat Table from Pivot Tables
extract_labels

Extract labels