[R] the underscore ("_") in variable name

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Oct 2 10:19:13 CEST 2000


On Mon, 2 Oct 2000, mohd zamri wrote:

> new to R and starting to learn to program R. The underscore ("_") did some
> suprising result. e.g
> 
> > c <- c(1,2,3,4,5)
> > mean(c)
> [1] 3
> > c_mean <- mean(c)
> > c
> [1] 3
> 
> having some experience in C, I thought the underscore is "always" valid in
> variable name. totally confuse me. Reread the R-FAQ a few times, and found

R is not C, and the set of allowed names is not at all the same.

> on pg. 7 foot-note which states that;
> 
>    the underscore character "_" is an alllowable synonym for the left
>    pointing assignment operator "<-", ...........
>    
> it is appear that R parse  "c_mean <-  mean(c)" as "c <- mean <- mean(c)",
> which explain the result.
> 
> suggestion:
> 
> I believe it would be helpful if this point be make clear on the FAQ, and
> not mention as a footnote. What is a proper naming convention and a few
> example of valid/invalid name for variables.

It would be helpful to give a naming convention, but this is obscured by
the fact that names can often be quoted.  So "c_mean" *is* a valid name.

> "c_mean" <- mean(1:5)
> get("c_mean")
[1] 3

Names which can be used without quoting them contain A-Z, a-z, 0-9 and .,
but may not start with 0-9 and .

The definition of A-Z, a-z is locale-dependent, that is, it is all chars
that are considered to be alphabetic in the locale.  That may not be what
you expect, especially on Windows (1.2.0 will be better).

Unfortunately, the only place this seems to be written down is in the
unfinished (and unreleased) R Language Manual and in various places in
the Venables & Ripley books.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list