dplyr::select() Accepts Characters Since Version 0.7.0

October 25, 2017 by Hiroaki Yutani

Some of my friends didn’t aware that dplyr now accepts characters. Did you?

For example, this expression

select(iris, Sepal.Length, Petal.Length)

can be also written in this way:

select(iris, "Sepal.Length", "Petal.Length")

or in this way:

select(iris, c("Sepal.Length", "Petal.Length"))

For the semantics of select(), you can find a good explanation in the vignette.

Tidyeval?

You may want to write some code that selects columns programmatically using variables.

x <- c("col1", "col2")
select(some_data, x)

But, be fareful, x can be ambiguous. If some_data has a column named x, it selects the column x instead of col1 and col2, the content of the variable x.

To avoid these troubles, you should explicitly unquote x by !! or !!! (both are ok in this case).

x <- c("col1", "col2")
select(some_data, !! x)

rlang::expr() can show how the expression is unquoted.

x <- c("col1", "col2")

rlang::expr(select(some_data, !! x))
#> select(some_data, c("col1", "col2"))

rlang::expr(select(some_data, !!! x))
#> select(some_data, "col1", "col2")