Skip to contents

Creates a data object with ID, weight, and additional columns. If multiple columns share the same prefix (before a colon), they are automatically "packed" into a single multi-coded field when all of them are logical. Columns that were not packed are converted to factors in the final dataset. Optionally, a dictionary may be used to apply descriptive labels to columns.

Usage

create_filter_data(data, id_col, weight_col = NULL, dictionary = NULL)

Arguments

data

A data frame (or tibble) containing the survey data.

id_col

A bare column name or tidyselect specification identifying the unique ID column in data. This column must be strictly unique (no duplicates).

weight_col

An optional bare column name or tidyselect specification identifying a weight column in data. If omitted, a column of weight 1 will be created for all rows.

dictionary

An optional data frame used to label columns. It should contain at least two columns: "alias" for the original column names and "ai_name" for their descriptive labels. If a column name is not found in dictionary, a message is displayed. If you are working with {g6tr.datalake}, you can load in survey dictionary by running g6tr.datalake::dl_read_rds('lzl/srv_dictionary', project = ..)

Value

A tibble (or data frame) with class c("filter_data", ...).

Details

  1. Packing columns: If two or more columns share the same prefix before a colon (e.g., "var:1", "var:2"), those columns are combined only if all such columns are logical. If any are non-logical, the process is stopped and you have to review those columns before trying again.

  2. Non-packed columns become factors: After all possible packing is done, any remaining columns (besides id, weight, and newly created packed columns) are converted to factor.

  3. Labeling: If dictionary is provided, each column is checked for a matching alias. If found, a "label" attribute is attached to the column with the corresponding ai_name.

Examples

if (FALSE) { # \dontrun{
library(dplyr)

# Example data
data <- tibble(
  id = 1:5,
  weight = c(1, 2, 1, 3, 2),
  gender = factor(c("Male", "Female", "Male", "Female", "Male")),
  region = factor(c("North", "South", "East", "West", NA)),
  `s_race:White` = c(T, F, T, T, F),
  `s_race:Black` = c(F, F, T, T, F),
  `s_race:Asian` = c(T, F, F, T, F)
)

# Example dictionary
dict <- data.frame(
  alias = c("gender", "s_race"),
  ai_name = c("Gender", "Race (MR)")
)

# Create filter_data
filter_data <- create_filter_data(
  data = data,
  id_col = id,
  weight_col = weight,
  dictionary = dict
)

# Inspect the result
filter_data

# Check the column labels
attr(filter_data$gender, "label")
attr(filter_data$region, "label")

## Manually update a column
attr(filter_data$region, "label") <- "Region"

filter_data

## In a case where you don't want to show all levels of a column as filters,
## You can explicitly set what to show while keeping the data intact.

filter_data <- filter_data |> mutate(region =  set_show_only(region, c("North", "South")))
filter_data
} # }