Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I use `all_of` to select columns?

Tags:

People also ask

Which function do you use for selecting columns?

To pick out single or multiple columns use the select() function. The select() function expects a dataframe as it's first input ('argument', in R language), followed by the names of the columns you want to extract with a comma between each name.

How do you select columns in Tidyverse?

The second way to select a column from a dataframe is to use the pipe operator %>% available as part of tidyverse. Here we first specify the name of the dataframe we want to work with and use the pipe %>% operator followed by select function with the column name we want to select.


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?