There are many R questions related to "What is a valid/invalid identifier" / "Why do read/make.names modify syntactically invalid identifiers?" / "How to control/customize handling of invalid characters?" but no canonical. None of the questions listed at bottom seems anywhere near general enough. So is this a case for a big community wiki-answer?
The tl;dr on syntactically valid/invalid identifiers in R is:
- What constitutes a valid symbol (identifier) in R
- A syntactically valid identifier consists of letters, numbers and dot or underline characters and starts with a letter or the dot not followed by a number.
- Names such as ".2way" are not valid identifiers, and neither are reserved words. The definition of a 'letter' depends on the current locale (may include accented letters), but only ASCII digits are considered to be digits. See the regex syntax
[alnum]
- Hence, syntactically valid identifiers cannot contain spaces, whitespace or any other punctuation
,:;+-*/~``!@#$%^&=<>?()[]{}|"'
or non-alphanumeric character. - Names starting with '.' are valid but are treated specially and hidden in
ls()
, unless you dols(all.names=T)
. By convention names starting with '.' are only used for internal or special functions/ function-arguments, not user-declared ones. - (Objects can have names that are not identifiers. This is very restrictive. These are generally only accessed via
get
/assign
not=
/<-
... and do not recognise subscripting operators or replacement functions. Or they can be accessed via `` backticks /deparse
)
data.frame
by default enforcescheck.names=T
on col-names: 'Specifying column names in a data.frame changes spaces to “.”'By convention, names with internal dots:
fac.A,fac.B,...
/fac.1,fac.2,...
usually signify factors being converted to contrasts, or other hierarchy: e.g.contrast
/model.matrix
/lm
/cast
/reshape
/...in File I/O:
read.csv/read.table
by default use optioncheck.names=T
which makes them callmake.names
.- It is allowed for column-names and row-names to be syntactically invalid. So if you need to handle syntactically invalid col- or rownames:
- ...to preserve them during reading, use option
check.names=F
- use quotes in select operation dplyr: select column names containing white space
- ...to preserve them during reading, use option
data.table::fread()
also has acheck.names
parameter but it defaults to FALSE : fread from data.table package when column names include spaces and special characters?- [Discussion of name treatment in any other file-reading / file-writing functions or packages]
- It can be less grief to simply set some/all column-names after reading with
setNames
(/setnames/set_names
). - In
fread()
, if names are illegal or duplicate, parameterscolClasses
,select
should either reference the pre-check.names-names, or else be an unnamed vector fread apply select and colClasses after check.names?
- The above command default options can be changed [insert reference]
- Other important package breakage/workarounds from invalid names:
- Workarounds to ggplot breakage on columns that have spaces in their names
dplyr::select(a:f)
selects multiple columns, but if the column-names are illegal (e.g. "1","2",..."8"), you need the SE versionselect_
: Select multiple columns with dplyr::select() with numbers as names
None of the questions seems general enough to be canonical:
make.names
does not allow any customizing of the table of allowed characters and their corresponding replacements, so if you need to do that, brew your own: Customizing make.names function in R?- Hence one option is to set
check.names=FALSE
inread.csv
How to replace the “.” in column names generated by read.csv() with a single space when exporting? - Colons inside names are illegal Why doesn't make.names() function keep the original names?
- Non-visible illegal characters in the header row can confuse read commands and result in the default
X.1,X.2,...
colnames Why am I getting X. in my column names when reading a data frame? .
by itself is a legal name, hence the so-called base-R "Bizarro pipe" syntax2 ->.; exp(.)
I see it, but I don't believe it. Legal names in R, piping operations, and the dot.
syntax to quote variable names e.g..(foo,bar,baz)
is used in plyr and data.table. In plyr,.
is actually declared as a function.
get
,assign
etc by placing them in backticks, e.g.`a thing` <- 2; `a thing` + 2
deparse
the same thing?`x` <- 3
is a legitimate R command anddeparse(x) <- 3
isn't.data.table
,.()
is a synonym forlist()
..()
syntax from plyr which first used it. data.table does not apparently actually declare.
as a function, it just understands the syntax.[
indata.table
.foo
for classdata.frame
or (at least in theory) a method forfoo.data
for classframe
.