list_xtab_args: Generate multiple lists of crosstab arguments

View source: R/list_xtab_args.R

list_xtab_argsR Documentation

Generate multiple lists of crosstab arguments

Description

Simply put, list_xtab_args() is a vectorized version of generate_xtab_args(). The function generate_xtab_args() allows for one var_of_interest at a time. There may be use cases where multiple var_of_interest must be considered. In those cases, this function may be useful as it allows users to pass a vector of var_of_interest and a corresponding list of dependent_vars. These two vectors must be of the same length in terms of vec_size(). One may think of these two vectors as pairs in the sence that— each ith element in var_of_interest is crossed with each element of dependent_vars, which is a vector in and of itself. If no dependent_vars list is specified, the function defaults to crossing each variable in var_of_interest with all other columns in df unless rm is specified.

Usage

list_xtab_args(df, var_of_interest, dependent_vars = NULL, rm = NULL)

Arguments

df

A data frame or tibble.

var_of_interest

A character vector of variables of interest.

dependent_vars

A list of character vectors, each of which contains dependent variables. Defaults to crossing each element of var_of_interest with all other variables in df if rm is NULL.

rm

A character vector of variables to be removed from consideration. Defaults to NULL.

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_args(), if it suceeds. The error list captures all runs of generate_xtab_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 var_of_interest failed by examining the error list and running the following code (assuming that the list is called list_of_args and the vector of variables of interest is called var_of_interest):

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

To examine the results of the successful runs, use:

        list_of_args[["result"]][lgl_index]
  

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

        list_of_xtab_args <- list_of_args[["result"]]
  

Examples


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

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

# Create list_of_args
list_of_args <- list_xtabs_args(df, var_of_interest, dependent_vars)


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