Learn R Programming

tidytable (version 0.5.7)

pivot_wider.: Pivot data from long to wide

Description

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

Usage

pivot_wider.(
  .df,
  names_from = name,
  values_from = value,
  id_cols = NULL,
  names_sep = "_",
  values_fn = NULL,
  values_fill = NULL
)

Arguments

.df

A data.frame or data.table

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). tidyselect compatible.

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). tidyselect compatible.

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. tidyselect compatible.

names_sep

the separator between the names of the columns

values_fn

Should the data be aggregated before casting? If the formula doesn't identify a single observation for each cell, then aggregation defaults to length with a message.

values_fill

If values are missing, what value should be filled in

Examples

Run this code
# NOT RUN {
test_df <- data.table(
  z = rep(c("a", "b", "c"), 2),
  stuff = c(rep("x", 3), rep("y", 3)),
  things = 1:6)

test_df %>%
  pivot_wider.(names_from = stuff, values_from = things)

test_df %>%
  pivot_wider.(names_from = stuff, values_from = things, id_cols = z)

# }

Run the code above in your browser using DataLab