long_to_wide {RcensusPkg}R Documentation

long_to_wide

Description

Reshape a data.table from a "long" format to a "wide" format.

Function calls data.table::dcast() to reshape a long single column and its values to multiple columns.

Usage

long_to_wide(
  dt = NULL,
  id_v = c("NAME", "GEOID"),
  parameter_col = NULL,
  value_col = NULL
)

Arguments

dt

The required data.table with a long column format.

id_v

A required vector of column names from 'dt' that act as identifiers and are not part of the widened column.

parameter_col

A column name from 'dt' whose unique values will become column names for the new expanded data.table.

value_col

A required column name or vector of column names from 'dt' whose values will fall under the new expanded data.table.

Value

A reshaped data.table in the "wide" format.

Examples

## Not run: 
  # Requires Census Bureau API key
  library(data.table)
  library(httr2)
  library(jsonlite)
  library(stringr)
  library(RcensusPkg)

  # Request for data from Census Bureau in "long" form
  B19001_1yr_long_dt <- RcensusPkg::get_vintage_data(
    dataset = "acs/acs1",
    vintage = 2016,
    group = "B19001",
    region = "state",
    wide_to_long = TRUE
  )

  # Resulting data.table is in the "long" form. Convert it back to
  # to the wide form.
  B19001_1yr_wide_dt <- RcensusPkg::long_to_wide(
    dt = B19001_1yr_long_dt,
    parameter_col = "variable",
    value_col = c("estimate", "moe")
  )

## End(Not run)

[Package RcensusPkg version 0.1.5 Index]