Learn R Programming

blockr.dplyr (version 0.1.0)

new_pivot_wider_block: Pivot Wider block constructor

Description

This block reshapes data from long to wide format by pivoting column values into new columns (see tidyr::pivot_wider()). This is the inverse operation of pivot_longer.

Usage

new_pivot_wider_block(
  names_from = character(),
  values_from = character(),
  id_cols = character(),
  values_fill = "",
  names_sep = "_",
  names_prefix = "",
  ...
)

Value

A block object for pivot_wider operations

Arguments

names_from

Character vector specifying which column(s) to use for new column names. Can be a single column or multiple columns.

values_from

Character vector specifying which column(s) to use for cell values. Can be a single column or multiple columns.

id_cols

Character vector of columns that uniquely identify each row. If empty (default), uses all columns not specified in names_from or values_from.

values_fill

Optional value to use for missing combinations. Can be a single value like "0" or "NA". Leave empty to keep missing values as NA.

names_sep

Separator to use when names_from specifies multiple columns. Default is "_".

names_prefix

Optional prefix to add to all new column names.

...

Additional arguments forwarded to blockr.core::new_transform_block()

See Also

blockr.core::new_transform_block(), tidyr::pivot_wider()

Examples

Run this code
# Create a pivot wider block
new_pivot_wider_block()

if (interactive()) {
  # Basic usage with long format data
  library(blockr.core)
  long_data <- data.frame(
    id = rep(1:3, each = 3),
    measurement_type = rep(c("a", "b", "c"), 3),
    value = c(10, 15, 12, 20, 25, 22, 30, 35, 32)
  )
  serve(
    new_pivot_wider_block(
      names_from = "measurement_type",
      values_from = "value"
    ),
    data = list(data = long_data)
  )

  # With values_fill to replace NAs
  serve(
    new_pivot_wider_block(
      names_from = "measurement_type",
      values_from = "value",
      values_fill = "0"
    ),
    data = list(data = long_data)
  )

  # With custom names_prefix
  serve(
    new_pivot_wider_block(
      names_from = "measurement_type",
      values_from = "value",
      names_prefix = "measure_"
    ),
    data = list(data = long_data)
  )
}

Run the code above in your browser using DataLab