How to use dplyr SE with "invalid" names (ie containing spaces)?

1k Views Asked by At

I can't figure out how to use SE dplyr function with invalid variable names, for example selecting a variable with a space in it.

Example:

df <- dplyr::data_frame(`a b` = 1)
myvar <- "a b"

If I want to select a b variable, I can do it with dplyr::select(df, `a b`), but how do I do that with select_?

I suppose I just need to find a function that "wraps" a string in backticks, so that I can call dplyr::select_(df, backtick(myvar))

2

There are 2 best solutions below

0
On BEST ANSWER

As MyFlick said in the comments, this behaviour should generally be avoided, but if you want to make it work you can make your own backtick wrapper

backtick <- function(x) paste0("`", x, "`")
dplyr::select_(df, backtick(myvar))

EDIT: Hadley replied to my tweets about this and showed me that simply using as.name will work for this instead of using backticks:

df <- dplyr::data_frame(`a b` = 1)
myvar <- "a b"
dplyr::select_(df, as.name(myvar))
0
On

My solution was to exploit the ability of select to use column positions. The as.name solution did not appear to work for some of my columns.

select(df, which(names(df) %in% myvar))

or even more succinctly if already in a pipe:

df %>%
 select(which(names(.) %in% myvar))

Although this uses select, in my view it does not rely on NSE.

Note that if there are no matches, all columns will be dropped with no error or warning.