How to use dplyr SE with "invalid" names (i.e. with spaces)?

I cannot figure out how to use the SE dplyr function with invalid variable names, for example, choosing a variable with a space in it.

Example:

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

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

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

+5
source share
2 answers

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

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

EDIT: Hadley answered my tweets about this and showed me that just using as.name would work for this instead of using the reverse steps:

 df <- dplyr::data_frame(`ab` = 1) myvar <- "ab" dplyr::select_(df, as.name(myvar)) 
+7
source

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

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

or even more succinctly, if already in the pipe:

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

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

Please note that if there are no matches, all columns will be deleted without errors or warnings.

+2
source

All Articles