wide_to_long {RcensusPkg}R Documentation

wide_to_long

Description

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

Function is a helper in calling data.table's melt() function to reshape a wide data frame to a long form.

Usage

wide_to_long(
  dt = NULL,
  id_v = c("NAME", "GEOID"),
  measure_v = NULL,
  variable_name = "variable",
  value_name = "estimate",
  na_rm = FALSE
)

Arguments

dt

The data frame with a wide collection of column variables. This parameter is required.

id_v

A character vector of column from dt that are not to be consolidated and act as identifier columns for the new long form. This parameter is required.

measure_v

An optional character vector that sets the column measures from dt that are to be consolidated. If not specified then all the columns not in id_v are considered measures and will be consolidated.

variable_name

An optional string that sets the column name for the consolidated column names.

value_name

An optional string that sets the column name for the consolidated values.

na_rm

An optional logical which if TRUE will remove rows with NA values.

Value

A reshaped data frame in the "long" format.

Examples

## Not run: 
  # Requires Census Bureau API key
  library(data.table)
  library(downloader)
  library(jsonlite)
  library(RcensusPkg)
  # Request for data from Census Bureau which comes in the "wide" form
  B19001_1yr_wide_dt <- RcensusPkg::get_vintage_data(
    dataset = "acs/acs1",
    vintage = 2016,
    group = "B19001",
    region = "state"
  )
  # Convert the returned data.table into "long" form
  B19001_1yr_long_dt <- RcensusPkg::wide_to_long(
    dt = B19001_1yr_wide_dt,
    id_v = c("NAME","GEOID")
  )

## End(Not run)

[Package RcensusPkg version 0.1.5 Index]