Importing data with a header row using read.delim, one variable should be named @5HTT but it is automatically renamed to X.5HTT, presumably because the "@" is either unacceptable or misunderstood. I've tried to find out what the rules are on variable names but have been unsuccessful. I'll bet someone here can tell me where to look. Maybe it's hidden away in here somewhere: http://cran.r-project.org/doc/manuals/R-data.pdf Thanks in advance. Mike
Krzysztof Sakrejda-Leavitt
2009-Mar-27 01:31 UTC
[R] use of "@" character in variable name
The '@' character is an operator used for accessing slots in S4 classes. Similarly the '$' character is the operator for accessing elements of lists, etc... Although R allows periods, SQL databases will choke on them. LaTeX will (sometimes?) choke on underscores, and of course any native R operator will cause problems. My take is that camelBackCapitalization is the best practice in naming columns, list elements, variables, and functions for R. Best, Krzysztof. Mike Miller wrote:> Importing data with a header row using read.delim, one variable should > be named @5HTT but it is automatically renamed to X.5HTT, presumably > because the "@" is either unacceptable or misunderstood. I've tried to > find out what the rules are on variable names but have been > unsuccessful. I'll bet someone here can tell me where to look. Maybe > it's hidden away in here somewhere: > > http://cran.r-project.org/doc/manuals/R-data.pdf > > Thanks in advance. > > Mike > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- ----------------------------------------------- Krzysztof Sakrejda-Leavitt Organismic and Evolutionary Biology University of Massachusetts, Amherst 319 Morrill Science Center South 611 N. Pleasant Street Amherst, MA 01003 work #: 413-325-6555 email: sakrejda at nsm.umass.edu
On 27/03/2009, at 2:04 PM, Mike Miller wrote:> Importing data with a header row using read.delim, one variable > should be > named @5HTT but it is automatically renamed to X.5HTT, presumably > because > the "@" is either unacceptable or misunderstood. I've tried to > find out > what the rules are on variable names but have been unsuccessful. > I'll bet > someone here can tell me where to look. Maybe it's hidden away in > here > somewhere: > > http://cran.r-project.org/doc/manuals/R-data.pdfI don't know if there is a comprehensive list of the rules governing variable names but the ``@'' sign is used to access ``slots'' under S4 classes and methods. See ?"@". So it is (like?) an operation/ operator and hence is ruled out just like ``+5HTT'' would be. Reserved words like ``break'' and ``while'' are also excluded. See fortune(18). Another rule is that a variable name can't begin with a digit. And it can't have white space in it. There are probably other rules, but essentially anything *sensible* as a variable name can be used as a variable name. cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
Dear Mike, As a slight simplification, a legal R name can start with a period (.), upper- or lower-case letter (A-Z, a-z), and can contain periods, underscores (_), letters, and numerals; depending upon the locale, some other characters may also be allowed. This information *is* in the R manuals, though it might not be that easy to locate: See section 10.3.2 of the R Language Definition manual or 1.8 of the Introduction to R manual. @ is used to access slots in an S4 object. I hope this helps, John> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]On> Behalf Of Mike Miller > Sent: March-26-09 9:04 PM > To: R-Help List > Subject: [R] use of "@" character in variable name > > Importing data with a header row using read.delim, one variable should be > named @5HTT but it is automatically renamed to X.5HTT, presumably because > the "@" is either unacceptable or misunderstood. I've tried to find out > what the rules are on variable names but have been unsuccessful. I'll bet > someone here can tell me where to look. Maybe it's hidden away in here > somewhere: > > http://cran.r-project.org/doc/manuals/R-data.pdf > > Thanks in advance. > > Mike > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
On Thu, 26 Mar 2009, Mike Miller wrote:> Importing data with a header row using read.delim, one variable should be named > @5HTT but it is automatically renamed to X.5HTT, presumably because the "@" is > either unacceptable or misunderstood. I've tried to find out what the rules > are on variable names but have been unsuccessful. I'll bet someone here can > tell me where to look. Maybe it's hidden away in here somewhere: > > http://cran.r-project.org/doc/manuals/R-data.pdfIt's hidden away in: FAQ 7.14 What are valid names? -thomas Thomas Lumley Assoc. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle
Its possible to have otherwise illegal names by surrounding them in backticks:> `a at b` <- 3 > `a at b`[1] 3> `if` <- 55 > `if`[1] 55 Also note use of check.names=FALSE:> Lines <- "a at b if+ 1 2"> DF <- read.delim(textConnection(Lines), header = TRUE, check.names = FALSE) > DFa at b if 1 1 2 On Thu, Mar 26, 2009 at 9:04 PM, Mike Miller <mbmiller+l at gmail.com> wrote:> Importing data with a header row using read.delim, one variable should be > named @5HTT but it is automatically renamed to X.5HTT, presumably because > the "@" is either unacceptable or misunderstood. ?I've tried to find out > what the rules are on variable names but have been unsuccessful. ?I'll bet > someone here can tell me where to look. ?Maybe it's hidden away in here > somewhere: > > http://cran.r-project.org/doc/manuals/R-data.pdf > > Thanks in advance. > > Mike > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >