dplyr: nonstandard column names (white space, punctuation, starts with numbers)
You may select
the variable by using backticks `
.
select(df, `a a`)
# a a
# 1 1
# 2 2
# 3 3
However, if your main objective is to rename the column, you may use rename
in plyr
package, in which you can use both ""
and ``
.
rename(df, replace = c("a a" = "a"))
rename(df, replace = c(`a a` = "a"))
Or in base
R:
names(df)[names(df) == "a a"] <- "a"
For a more thorough description on the use of various quotes, see ?Quotes
. The 'Names and Identifiers' section is especially relevant here:
other [syntactically invalid] names can be used provided they are quoted. The preferred quote is the backtick".
See also ?make.names
about valid names.
See also this post about renaming in dplyr
Some alternatives to backticks, good as of dplyr 0.5.0, the current version as of this writing.
If you're trying to programmatically select an argument as a column and you don't want to rename or do something like paste
/sprintf
the column name into backticks, you can use as.name
in conjunction with the non-standard evaluation version of select
, which is select_
:
dplyr::select_(df, as.name("a a"))
Many of the dplyr
functions have non-standard versions. In the case of select
specifically, you can also use the standard version in conjunction with the select helper one_of
. See ?dplyr::select_helpers
for documentation:
dplyr::select(df, dplyr::one_of("a a"))