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

Bill Venables Bill.Venables at cmis.csiro.au
Mon Oct 2 08:08:03 CEST 2000

At 14:35 02/10/00 +0800, 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
>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.
>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.

Ah, but now that you have been bitten by it, I bet you don't make the same
mistake again!  :-)

There is a no-win situation here.  It is there (I presume) mainly for
compatibility with S.  It is in S because some old keyboards did not have
an underscore but a left-pointing arrow at that same position.  For better
or worse we are stuck with it now.

If we follow your suggestion and give prominence to the fact that "_" is a
synonym for "<-" then as sure as eggs some people will actually USE it!
This is, in the opinion of many of us at least, a very BAD thing as it
makes code just about unreadible.  On the other hand if we don't give
prominence to the fact but just hide it in an obscure footnote, then many
people will get bitten first time (but only then) and come complaining to
R-help and I will write more tetchy little notes like this one...


Bill Venables.
Bill Venables, Statistician                         Tel. +61 7 3826 7251 
CSIRO Marine Laboratories,                          Fax. +61 7 3826 7304
Cleveland, Qld, 4163                  Email: Bill.Venables at cmis.csiro.au
AUSTRALIA                        http://www.cmis.csiro.au/bill.venables/

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