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

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Oct 2 11:36:07 CEST 2000

```> From: Jacob Anhoj <anhoej at dadlnet.dk>
> Date: Mon, 2 Oct 2000 11:15:09 +0200
>
> don't know much about programming. But I know that it is not a good idea to
> assign a variable name to the name of an intrinsic constant or function. In
> your example, you assign the name "c" to a variable. But "c" is already the
> name of a function i R. In this way you are asking for trouble.

Perhaps, but R knows the difference between functions and (numeric)
variables, so you will get into less trouble than you might expect.

The function conflicts() (see its help page) can help detect if this
is the problem.  The real difficulty seems to come from using a modified
version of a system function, and forgetting about it.

> -----Original Message-----
> From: owner-r-help at stat.math.ethz.ch
> [mailto:owner-r-help at stat.math.ethz.ch]On Behalf Of mohd zamri
> Sent: 2. oktober 2000 08:36
> To: r-help at stat.math.ethz.ch
> Subject: [R] the underscore ("_") in variable name
>
>
> 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
> 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.
>
> zamri
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> -.-
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> _._
>
>
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> 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
>
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

--
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```