[R] using reserved words in R, and reuse variable names in different functions

Rolf Turner rolf.turner at xtra.co.nz
Sat Mar 2 14:00:55 CET 2013


On 03/02/2013 01:12 PM, Sarah Goslee wrote:
> On Fri, Mar 1, 2013 at 7:06 PM, C W <tmrsg11 at gmail.com> wrote:
>> Thanks, that was just an example I came up with.  I was just curious if
>> using same variable names in different functions would cause problems.
> No. The environment of a function is independent of other functions.
>
>>   Especially with reserved words.
> Yes. Using reserved words can cause all kinds of subtle problems. Avoid it.
>

Very sound advice.  But it should be noted that "t", "c", and "matrix" 
to which
the OP referred are *not* technically reserved words.  Nonetheless their use
as names of user-defined objects should be eschewed.  See fortune("dog").

You *can't* actually assign values to reserved words.  E.g.

     TRUE <- 42

throws an error.  (Whereas matrix <- 42, bad form though it may be,
does not throw an error.)

     cheers,

         Rolf Turner



More information about the R-help mailing list