dt_pivot_wider | R Documentation |
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)
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 |
A pair of arguments describing which column (or columns) to get the name of the output column ( |
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 ( |
A reshaped data.table into wider format
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)
dt_pivot_wider(example_dt, names_from = stuff, values_from = things, id_cols = z)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.