[R] using reserved words in R, and reuse variable names in different functions
Sarah Goslee
sarah.goslee at gmail.com
Sat Mar 2 01:12:59 CET 2013
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.
Sarah
>
> Mike
>
> On Fri, Mar 1, 2013 at 5:45 PM, David Winsemius <dwinsemius at comcast.net>wrote:
>
>>
>> On Mar 1, 2013, at 1:56 PM, C W wrote:
>>
>> > Hi list,
>> > I am writing several functions and running out variable names. I am
>> using
>> > words such as "t", "c", "matrix" to keep the notation same as formulas I
>> am
>> > using.
>> >
>> > For example I have,
>> >
>> > unnormalized <- function(t, x, y){
>> > val <- rnorm(t, mean=x, var=y)
>> > return(val)
>> > }
>> >
>> > metropolis <- function(t, c, x, y){
>> > den1 <- unnormalized(t, mean=x, sd=y)
>> > den2 <- unnormalized(c, mean=x, sd=y)
>> > if(den1 < den2)
>> > return(a)
>> > else
>> > return(b)
>> > }
>> >
>> > for(i in 1: 100){
>> > matrix <- c()
>> > matrix[i] <- metroplis(1, 2, 3, 4)
>> > }
>> > Here, I reused letter "t" and "c", and the word "matrix". Could this
>> cause
>> > any potential problems?
>>
>> Whatever problems you are having at the moment (and they appear to be
>> many) are not due to using existing function names as data-object names.
>> You are asked to report the error messages you get with your code problems,
>> and those messages I found to be reasonably informative for the first 5
>> errors I found.
--
Sarah Goslee
http://www.functionaldiversity.org
More information about the R-help
mailing list