"Widens" data, increasing the number of columns and decreasing the number of rows.

pivot_wider.(
  .df,
  names_from = name,
  values_from = value,
  id_cols = NULL,
  names_sep = "_",
  names_prefix = "",
  names_glue = NULL,
  names_sort = FALSE,
  names_repair = "unique",
  values_fill = NULL,
  values_fn = 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

names_prefix

prefix to add to the names of the new columns

names_glue

Instead of using names_sep and names_prefix, you can supply a glue specification that uses the names_from columns (and special .value) to create custom column names

names_sort

Should the resulting new columns be sorted.

names_repair

Treatment of duplicate names. See ?vctrs::vec_as_names for options/details.

values_fill

If values are missing, what value should be filled in

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.

Examples

df <- tidytable(
  id = 1,
  names = c("a", "b", "c"),
  vals = 1:3
)

df %>%
  pivot_wider(names_from = names, values_from = vals)
#> # A tidytable: 1 × 4
#>      id     a     b     c
#>   <dbl> <int> <int> <int>
#> 1     1     1     2     3

df %>%
  pivot_wider(
    names_from = names, values_from = vals, names_prefix = "new_"
  )
#> # A tidytable: 1 × 4
#>      id new_a new_b new_c
#>   <dbl> <int> <int> <int>
#> 1     1     1     2     3