list_xtab_3way_args: Generate multiple lists of three-way crosstab arguments

View source: R/list_xtab_args.R

list_xtab_3way_argsR Documentation

Generate multiple lists of three-way crosstab arguments

Description

Simply put, list_xtab_3way_args() is a vectorized version of generate_xtab_3way_args(). The function generate_xtab_3way_args() allows for one control_var at a time. There may be use cases where multiple control_var must be considered. In those cases, this function may be useful as it allows users to pass a vector of control_var and corresponding lists of independent_vars and dependent_vars. These three vectors must be of the same length in terms of vec_size(). One may think of these three vectors as trios in the sence that— each ith element in control_var is crossed with each elements of independent_vars and dependent_vars, which are vectors in and of themselves.

Usage

list_xtab_3way_args(df, control_var, independent_vars, dependent_vars)

Arguments

df

A data frame or tibble.

control_var

A character vector of control variables.

independent_vars

A list of character vectors, each of which contains independent variables.

dependent_vars

A list of character vectors, each of which contains dependent variables.

Value

A list containing two elements— result and error, which are lists in and of themselves. These two lists have the same structure and number of elements. The result is a named list of tibble objects. One can think of each tibble as the output of a single run of generate_xtab_3way_args(), if it suceeds. The error list captures all runs of generate_xtab_3way_args() that have failed, returning the error messages. If all run fails, result will be a NULL list; on the other end, if no run fails, error will be a NULL list. Users can easily find out which control_var failed by examining the error list and running the following code (assuming that the list is called list_of_args_3way and the vector of control variables is called control_var):

        lgl_index <- purrr::map_lgl(list_of_args_3way[["error"]], is.null)
        control_var[!lgl_index]
  

To examine the results of the successful runs, use:

        list_of_args_3way[["result"]][lgl_index]
  

Or, if all runs are successful, simply return the result list:

        list_of_xtab_3way_args <- list_of_args_3way[["result"]]
  

Examples


# Create control_var vector
control_var <- c("col1", "col3", "col20", "col23")

# List of independent variable vectors
independent_vars <- list(
  c("col9", "col2", "col5", "col100"),
  c("col2", "col4"),
  c("col7", "col9", "col40", "col17"),
  c("col2", "col11")
)

# List of dependent variable vectors
dependent_vars <- list(
  c("col2", "col4", "col5", "col100"),
  c("col2", "col4"),
  c("col2", "col4", "col5", "col86"),
  c("col2", "col4")
)

# Create list_of_args_3way
list_of_args_3way <- list_xtab_3way_args(df, control_var, independent_vars, dependent_vars)


YangWu1227/citizenr documentation built on June 18, 2022, 12:17 p.m.