Skip to contents

Reshape a long format data.table using key-value pairs with data.table::dcast

Usage

dt_keybin_reshape(
  x,
  id_name,
  key_name,
  positive = 1,
  negative = 0,
  xname = NULL,
  verbosity = 1L
)

Arguments

x

data.table object.

id_name

Character: Name of column in x that defines the IDs identifying individual rows.

key_name

Character: Name of column in x that holds the key.

positive

Numeric or Character: Used to fill id ~ key combination present in the long format input x.

negative

Numeric or Character: Used to fill id ~ key combination NOT present in the long format input x.

xname

Character: Name of x to be used in messages.

verbosity

Integer: Verbosity level.

Value

data.table in wide format.

Author

EDG

Examples

library(data.table)
x <- data.table(
  ID = rep(1:3, each = 2),
  Dx = c("A", "C", "B", "C", "D", "A")
)
dt_keybin_reshape(x, id_name = "ID", key_name = "Dx")
#> 2026-02-22 18:59:25 
#> Reshaping x to wide format...
#>  [dt_keybin_reshape]
#>        Input size 6 x 3
#>       Output size 3 x 5
#> Key: <ID>
#>       ID     A     B     C     D
#>    <int> <int> <int> <int> <int>
#> 1:     1     1     0     1     0
#> 2:     2     0     1     1     0
#> 3:     3     1     0     0     1