Skip to contents

transform transaction -> master

Usage

transform_transaction_to_master(
  dat,
  ROW_NUMBER = "ROW_NUMBER",
  ID = "ID",
  collapse = ":",
  STATIC_NUM = NULL,
  STATIC_CHAR = NULL,
  DYNAMIC_NUM = NULL,
  DYNAMIC_CHAR = NULL
)

Arguments

dat

transaction data frame

ROW_NUMBER

column name for row number in the create data frame

ID

identifier name

collapse

separator used to join the per-ID values of a `_DIST` column (default ":"). It is handed to `paste(collapse = )` and is therefore a **literal string**. Pass the same value as `split` when the resulting `_DIST` column is later scored by [score_dist()]; that reads the column back with a literal separator too, so the two sides are symmetric and any character – including regex metacharacters such as `"|"` or `"."` – round-trips (Issue #32). Do not use a separator that occurs inside the values themselves.

STATIC_NUM

list of column name which shows STATIC NUMBER attribute

STATIC_CHAR

list of column name which shows STATIC CHARACTER attribute

DYNAMIC_NUM

list of column name which shows DYNAMIC NUMBER attribute

DYNAMIC_CHAR

list of column name which shows DYNAMIC CHARACTER attribute

Value

a data frame with one row per distinct `ID`, combining the STATIC_NUM/STATIC_CHAR columns as-is, `<col>_MAX`/`_MEAN`/`_MEDIAN`/ `_MIN` summaries of DYNAMIC_NUM, `collapse`-joined distributions (`<col>_DIST`) of DYNAMIC_NUM and DYNAMIC_CHAR, a ROWCOUNT column and the minimum `ROW_NUMBER` per `ID`.

Column naming

The aggregate columns are **always** named `<source column>_<statistic>`, whatever the number of columns given:

* `DYNAMIC_NUM` yields `<col>_MAX`, `<col>_MEAN`, `<col>_MEDIAN`, `<col>_MIN` for every column; * `DYNAMIC_NUM` and `DYNAMIC_CHAR` each additionally yield `<col>_DIST`.

This used to depend on how many columns were passed. `dplyr::summarise_all()` with a named function list only prefixes the result with the source column name when two or more columns survive grouping; with a single column it used the bare function names, so `DYNAMIC_NUM = "NUM_DYNAMIC"` produced `MAX`/`MEAN`/`MEDIAN`/`MIN` while `DYNAMIC_NUM = c("BIN", "NUM_DYNAMIC")` produced `BIN_MAX`/ `NUM_DYNAMIC_MAX`/... Downstream code therefore could not hard-code a column name (Issue #26).

The column *order* is unchanged: statistic-major (`<col1>_MAX, <col2>_MAX, ..., <col1>_MEAN, ...`), matching what `summarise_all()` produced for the multi-column case.

Examples

tran <- data.frame(
  ROW_NUMBER = 1:6,
  ID         = c("a", "a", "a", "b", "b", "b"),
  SEX        = c("M", "M", "M", "F", "F", "F"),
  AMOUNT     = c(100, 200, 300, 10, 20, 30)
)
m <- transform_transaction_to_master(tran, STATIC_CHAR = "SEX",
                                     DYNAMIC_NUM = "AMOUNT")
m
#> # A tibble: 2 × 9
#>   ID    SEX   AMOUNT_MAX AMOUNT_MEAN AMOUNT_MEDIAN AMOUNT_MIN AMOUNT_DIST
#>   <chr> <chr>      <dbl>       <dbl>         <dbl>      <dbl> <chr>      
#> 1 a     M            300         200           200        100 100:200:300
#> 2 b     F             30          20            20         10 10:20:30   
#> # ℹ 2 more variables: ROWCOUNT <int>, ROW_NUMBER <int>

# the aggregate columns are named <column>_<statistic> whatever the number
# of columns given (Issue #26), so downstream code can hard-code them
names(m)
#> [1] "ID"            "SEX"           "AMOUNT_MAX"    "AMOUNT_MEAN"  
#> [5] "AMOUNT_MEDIAN" "AMOUNT_MIN"    "AMOUNT_DIST"   "ROWCOUNT"     
#> [9] "ROW_NUMBER"   

# AMOUNT_DIST round-trips through score_dist() as long as
# `collapse` and `split` agree
m$AMOUNT_DIST
#> [1] "100:200:300" "10:20:30"