9

I'm currently using R and came across the function all_of in the tidyverse. What does this function exists for? It seems like I can use just x at every point where all_of(x) can be used..

Example:

library(tidyverse)

tb <- tibble(a=1:3, b=1:3, c=1:3)
x <- c("a", "b")

tb %>% select(all_of(x))
tb %>% select(x)

tb %>$ select(-all_of(x))
tb %>% select(-x)

The two lines with all_of yield the same return values as the ones without the extra function. Why should I bother using them?

s1624210
  • 627
  • 4
  • 11

1 Answers1

11

This is a really nice question!

It is to make it clear about what you really want when selecting columns. Imagine this simple situation:

library(tidyverse)

tb <- tibble(x = 1:3, y = 1:3, z = 1:3)
x <- c("x", "y")

tb %>% select(x)

Do you see that? It is not clear whether you want x as external vector here and thus select two columns (x and y), or if you want to select only one column x.

That's why you should use all_of(), which says that you want to select column names from an external vector.

More information can be found in tidyselect docs.

  • So `all_of` is of use, when I want to select columns stored in a variable whose name is a column as well. Should I use it always as good standard? – s1624210 Jun 15 '20 at 21:45
  • Exactly! And when refering to external vector, yes, you probably should use it as a good standard. I think that _not using_ it prints a warning message to the console. –  Jun 15 '20 at 21:49