cdata (version 1.0.7)

blocks_to_rowrecs: Map sets rows to columns (takes a data.frame).

Description

Transform data facts from rows into additional columns using controlTable.

Usage

blocks_to_rowrecs(tallTable, keyColumns, controlTable, ...,
  columnsToCopy = NULL, checkNames = TRUE, checkKeys = TRUE,
  strict = FALSE, controlTableKeys = colnames(controlTable)[[1]],
  tmp_name_source = wrapr::mk_tmp_name_source("bltrr"),
  temporary = TRUE)

# S3 method for default blocks_to_rowrecs(tallTable, keyColumns, controlTable, ..., columnsToCopy = NULL, checkNames = TRUE, checkKeys = TRUE, strict = FALSE, controlTableKeys = colnames(controlTable)[[1]], tmp_name_source = wrapr::mk_tmp_name_source("btrd"), temporary = TRUE)

# S3 method for relop blocks_to_rowrecs(tallTable, keyColumns, controlTable, ..., columnsToCopy = NULL, checkNames = TRUE, strict = FALSE, controlTableKeys = colnames(controlTable)[[1]], tmp_name_source = wrapr::mk_tmp_name_source("bltrr"), temporary = TRUE)

Arguments

tallTable

data.frame containing data to be mapped (in-memory data.frame).

keyColumns

character vector of column defining row groups

controlTable

table specifying mapping (local data frame)

...

force later arguments to be by name.

columnsToCopy

character, extra columns to copy.

checkNames

logical, if TRUE check names.

checkKeys

logical, if TRUE check keyColumns uniquely identify blocks (required).

strict

logical, if TRUE check control table name forms

controlTableKeys

character, which column names of the control table are considered to be keys.

tmp_name_source

a tempNameGenerator from cdata::mk_tmp_name_source()

temporary

logical, if TRUE use temporary tables

Value

wide table built by mapping key-grouped tallTable rows to one row per group

Details

This is using the theory of "fluid data"n (https://github.com/WinVector/cdata), which includes the principle that each data cell has coordinates independent of the storage details and storage detail dependent coordinates (usually row-id, column-id, and group-id) can be re-derived at will (the other principle is that there may not be "one true preferred data shape" and many re-shapings of data may be needed to match data to different algorithms and methods).

The controlTable defines the names of each data element in the two notations: the notation of the tall table (which is row oriented) and the notation of the wide table (which is column oriented). controlTable[ , 1] (the group label) cross colnames(controlTable) (the column labels) are names of data cells in the long form. controlTable[ , 2:ncol(controlTable)] (column labels) are names of data cells in the wide form. To get behavior similar to tidyr::gather/spread one builds the control table by running an appropriate query over the data.

Some discussion and examples can be found here: https://winvector.github.io/FluidData/FluidData.html and here https://github.com/WinVector/cdata.

See Also

build_pivot_control, rowrecs_to_blocks

Examples

Run this code
# NOT RUN {
  # pivot example
  d <- data.frame(meas = c('AUC', 'R2'),
                  val = c(0.6, 0.2))

  cT <- build_pivot_control(d,
                            columnToTakeKeysFrom= 'meas',
                            columnToTakeValuesFrom= 'val')
  blocks_to_rowrecs(d,
                    keyColumns = NULL,
                    controlTable = cT)


d <- data.frame(meas = c('AUC', 'R2'),
                val = c(0.6, 0.2))
cT <- build_pivot_control(
  d,
  columnToTakeKeysFrom= 'meas',
  columnToTakeValuesFrom= 'val')

ops <- rquery::local_td(d) %.>%
  blocks_to_rowrecs(.,
                    keyColumns = NULL,
                    controlTable = cT)
cat(format(ops))

if(requireNamespace("rqdatatable", quietly = TRUE)) {
  library("rqdatatable")
  d %.>%
    ops %.>%
    print(.)
}

if(requireNamespace("RSQLite", quietly = TRUE)) {
  db <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
  DBI::dbWriteTable(db,
                    'd',
                    d,
                    overwrite = TRUE,
                    temporary = TRUE)
  db %.>%
    ops %.>%
    print(.)
  DBI::dbDisconnect(db)
}

# }

Run the code above in your browser using DataCamp Workspace