Cross two vectors of variable names from a dataframe

cross2_var_vectors(data, x, y, verbose = FALSE)

Arguments

data

the dataframe or tibble the variables are contained in.

x, y

These are either character or integer vectors containing the names, e.g. "am" or the column numbers e.g. 9

verbose

the default is FALSE, setting to TRUE will cat additional output to the screen

Value

a list with two sublists `lista` and `listb`. Very handy for feeding the lists to `purrr` for further processing.

Author

Chuck Powell

Examples

cross2_var_vectors(mtcars, 9, c(2, 10:11))
#> $lista #> $lista[[1]] #> [1] "cyl" #> #> $lista[[2]] #> [1] "gear" #> #> $lista[[3]] #> [1] "carb" #> #> #> $listb #> $listb[[1]] #> [1] "am" #> #> $listb[[2]] #> [1] "am" #> #> $listb[[3]] #> [1] "am" #> #>
cross2_var_vectors(mtcars, "am", c("cyl", "gear", "carb"))
#> $lista #> $lista[[1]] #> [1] "cyl" #> #> $lista[[2]] #> [1] "gear" #> #> $lista[[3]] #> [1] "carb" #> #> #> $listb #> $listb[[1]] #> [1] "am" #> #> $listb[[2]] #> [1] "am" #> #> $listb[[3]] #> [1] "am" #> #>
x2 <- c("am", "carb") y2 <- c("vs", "cyl", "gear") cross2_var_vectors(mtcars, x2, y2, verbose = TRUE)
#> Pair 1 lista = vs listb = am #> Pair 2 lista = cyl listb = am #> Pair 3 lista = gear listb = am #> Pair 4 lista = vs listb = carb #> Pair 5 lista = cyl listb = carb #> Pair 6 lista = gear listb = carb
#> $lista #> $lista[[1]] #> [1] "vs" #> #> $lista[[2]] #> [1] "cyl" #> #> $lista[[3]] #> [1] "gear" #> #> $lista[[4]] #> [1] "vs" #> #> $lista[[5]] #> [1] "cyl" #> #> $lista[[6]] #> [1] "gear" #> #> #> $listb #> $listb[[1]] #> [1] "am" #> #> $listb[[2]] #> [1] "am" #> #> $listb[[3]] #> [1] "am" #> #> $listb[[4]] #> [1] "carb" #> #> $listb[[5]] #> [1] "carb" #> #> $listb[[6]] #> [1] "carb" #> #>
if (FALSE) { variables_list <- cross2_var_vectors(mtcars, x2, y2) mytitles <- stringr::str_c( stringr::str_to_title(variables_list$listb), " by ", stringr::str_to_title(variables_list$lista), " in mtcars data" ) purrr::pmap( .l = list( x = variables_list[[1]], # variables_list$lista y = variables_list[[2]], # variables_list$listb title = mytitles ), .f = CGPfunctions::PlotXTabs2, data = mtcars, ylab = NULL, perc.k = 1, palette = "Set2" ) }