dt_pivot_wider() "widens" data, increasing the number of columns and decreasing the number of rows. The inverse transformation is dt_pivot_longer(). Syntax based on the tidyr equivalents.

dt_pivot_wider(dt_, id_cols = NULL, names_from, names_sep = "_", values_from)

Arguments

dt_

the data table to widen

id_cols

A set of columns that uniquely identifies each observation. Defaults to all columns in the data table except for the columns specified in names_from and values_from. Typically used when you have additional variables that is directly related.

names_from

A pair of arguments describing which column (or columns) to get the name of the output column (name_from), and which column (or columns) to get the cell values from (values_from).

names_sep

the separator between the names of the columns

values_from

A pair of arguments describing which column (or columns) to get the name of the output column (name_from), and which column (or columns) to get the cell values from (values_from).

Value

A reshaped data.table into wider format

Examples


library(data.table)
example_dt <- data.table(
  z = rep(c("a", "b", "c"), 2),
  stuff = c(rep("x", 3), rep("y", 3)),
  things = 1:6
)

dt_pivot_wider(example_dt, names_from = stuff, values_from = things)
#> Key: <z>
#>         z     x     y
#>    <char> <int> <int>
#> 1:      a     1     4
#> 2:      b     2     5
#> 3:      c     3     6
dt_pivot_wider(example_dt, names_from = stuff, values_from = things, id_cols = z)
#> Key: <z>
#>         z     x     y
#>    <char> <int> <int>
#> 1:      a     1     4
#> 2:      b     2     5
#> 3:      c     3     6