Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

dplyr

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))

like image 304
DeanAttali Avatar asked Jun 11 '15 18:06

DeanAttali


2 Answers

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))
like image 192
DeanAttali Avatar answered Nov 11 '22 05:11

DeanAttali


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.

like image 30
Hugh Avatar answered Nov 11 '22 06:11

Hugh