C W
2013-Mar-01 21:56 UTC
[R] using reserved words in R, and reuse variable names in different functions
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? Thanks in advance, Mike [[alternative HTML version deleted]]
David Winsemius
2013-Mar-01 22:45 UTC
[R] using reserved words in R, and reuse variable names in different functions
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. -- David Winsemius Alameda, CA, USA
MacQueen, Don
2013-Mar-04 15:29 UTC
[R] using reserved words in R, and reuse variable names in different functions
Yes, it can cause problems. And speaking for myself, I'd say it's not worth the risk, because it's easy enough to find alternative variable names that are close enough to the notation of your formulas that remembering should be no problem. For example, "tt", "cc", and "mmatrix" might do it. -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 3/1/13 1:56 PM, "C W" <tmrsg11 at gmail.com> 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? Thanks in advance, Mike [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.