Skip to contents

Melts checkbox variables into a single variable

Usage

hts_melt_vars(
  shared_name = NULL,
  wide_dt = NULL,
  shared_name_vars = NULL,
  variables_dt = variable_list,
  data = hts_data,
  ids = c("hh_id", "person_id", "day_id", "trip_id", "vehicle_id"),
  remove_missing = TRUE,
  missing_values = c("Missing Response", "995"),
  checkbox_label_sep = ":",
  to_single_row = FALSE
)

Arguments

shared_name

Shared name of the checkbox variable to melt (e.g., 'race_1', and 'race_2' have shared name of 'race'). Defaults to NULL.

wide_dt

Table containing the checkbox variables to melt in data.table format.

shared_name_vars

List of the checkbox variables with the same shared name. Defaults to NULL.

variables_dt

List of variable locations and descriptions in data.table format.

data

List containing household, person, day, trip, and vehicle datasets in data.table format.

ids

unique identifiers appearing in wide_dt

remove_missing

Boolean to remove rows with missing values. Defaults to TRUE.

missing_values

Missing values to remove. Defaults to 'Missing Response' and 995.

checkbox_label_sep

Character to use to split description of checkbox variable in the variable list. Defaults to ':'

to_single_row

Boolean if treating multiple checkbox selections as 'Two or more'. Defaults to FALSE.

Value

Inputted data table with checkbox variables melted into a single variable, with a 'variable' column to indicate original variable names and a 'value' column with the original value of the checkbox variable.

Examples


require(data.table)
require(stringr)
hts_melt_vars(
  shared_name = "race",
  wide_dt = person,
  data = list(
    "hh" = hh,
    "person" = person,
    "day" = day,
    "trip" = trip,
    "vehicle" = vehicle
  )
)
#>        person_id hh_id variable value                      race
#>     1:         1   356   race_1     0 African American or Black
#>     2:         2   724   race_1     1 African American or Black
#>     3:         3   681   race_1     0 African American or Black
#>     4:         4   114   race_1     0 African American or Black
#>     5:         5   165   race_1     0 African American or Black
#>    ---                                                         
#> 14325:      2043   931 race_999     0      Prefer not to answer
#> 14326:      2044   667 race_999     1      Prefer not to answer
#> 14327:      2045   543 race_999     1      Prefer not to answer
#> 14328:      2046   749 race_999     1      Prefer not to answer
#> 14329:      2047   364 race_999     1      Prefer not to answer