Select variables

Dan Chaltiel

2021-03-05

This vignette will review the different ways of selecting variables to describe with crosstable. For more general informations about crosstable, see vignette("crosstable") (link). For more information and tips on tidyselect, see tidyselect syntax

Please note that tables look better on a white background. If you are using RStudio in dark mode, borders may look blurry.

Whole table

The simplest case is when you want to describe the whole table, as you need no further argument. If you really want to be more explicit, you also can use tidyselect::everything(). All tidyselect helpers are re-exported by dplyr so we only want to load this latter package.

Here are the 10 first lines of the mtcars2 dataset:

library(crosstable)
library(dplyr)
ct=crosstable(mtcars2, everything())
dim(ct)
#> [1] 46  4
ct %>% head(10) %>% 
  as_flextable(keep_id=TRUE)

Select by column name

Name

Just like with dplyr::select, you can use names with or without quotes to select variables you want to describe:

crosstable(mtcars2, mpg, by=vs) %>% 
  as_flextable(keep_id=TRUE)
crosstable(mtcars2, "qsec", by="am") %>% as_flextable(keep_id=TRUE)

External vector

However, it is better to use all_of or any_of when you take your column names from an external vector. Otherwise, there would be an ambiguity as you might have wanted to select a column named like this vector.

qsec=c("mpg", "cyl") #well, that's quite a bad variable name...
crosstable(mtcars2, any_of(qsec), by=vs) %>% 
  as_flextable(keep_id=TRUE)

Negation

You can also use negation to keep all but some columns:

crosstable(mtcars2, -mpg, by=vs) %>% head(7) %>% 
  as_flextable(keep_id=TRUE)
crosstable(mtcars2, -mpg, -cyl, by=vs) %>% head(8) %>% as_flextable(keep_id=TRUE)
crosstable(mtcars2, -c(mpg, cyl), by=vs) %>% head(8) %>% as_flextable(keep_id=TRUE)

Indice

This can be useful sometimes, for instance when you want to quickly describe the 3 first columns.

crosstable(mtcars2, 1:3, by=vs) %>% 
  as_flextable(keep_id=TRUE)

You can also use negation (-(1:3)), concatenation (c(1,2,3)), or both (crosstable(mtcars2, 1:4, -2, by=vs)).

Select with tidyselect helpers

Along with everything(), tidyselect provides a large choice of helpers. You can see ?tidyselect::select_helpers for a complete list. They all have the useful ignore.case argument which is often very convenient.

The main are re-exported by crosstable: starts_with, ends_with, contains and matches. Here are some examples:

crosstable(mtcars2, starts_with("d")) %>% 
  as_flextable(keep_id=TRUE)
crosstable(mtcars2, ends_with("g"), contains("yl")) %>% as_flextable(keep_id=TRUE)
#for regex haters, the following call selects all columns which name starts with "d" or "g", followed by exactly 3 characters crosstable(mtcars2, matches("^d|g.{3}$")) %>% as_flextable(keep_id=TRUE)

Select with predicate functions

Sometimes, you want to select columns if they meet a set of specifications, for instance of type or of value. You can then use predicate functions: functions that return a single logical value. If the function is named, it is a good practice to wrap it in where.

For instance, you might want to keep only numeric variables:

crosstable(mtcars2, where(is.numeric)) %>% 
  as_flextable(keep_id=TRUE)

Using anonymous functions, you can even use more complicated patterns. For instance, you might want only numeric variables which mean is higher than 100:

crosstable(mtcars2, function(x) is.numeric(x) && mean(x)>100) %>% 
  as_flextable(keep_id=TRUE)

Of note, crosstable support lambda-functions, so you could write crosstable(mtcars2, ~is.numeric(.x) && mean(.x)>100) for the exact same result.

The only, logical constraint is that the function should return a single logical value. Use &&, ||, and parenthesis to combine functions in complex patterns.

Select with a formula

If you want to mutate some variables in real-time, you can use the formula interface. The left-hand-side are the variables to describe, while the right-hand-side is the by variable (which can be set to NULL, 0 or 1 for “no variable”).

crosstable(mtcars2, mpg+cyl ~ vs) %>% 
  as_flextable(keep_id=TRUE)

This permits very complex and interesting patterns, using functions in situ and operations using with the I function. Labels are inherited and make little sense though.

crosstable(mtcars2, sqrt(mpg) + I(qsec^2) ~ ifelse(mpg>20,"mpg>20","mpg<20"),
           label=FALSE) %>% 
  as_flextable(keep_id=TRUE)

Note that you cannot use tidyselect helpers in formulas.

Ultimate example

Lets play a little with all of this :-)

I want all numeric variables that do not start by “d” or “w”, but I still want drat in the end.

crosstable(mtcars2, where(is.numeric), -matches("^d|w"), drat, label=FALSE) %>% 
  as_flextable()