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

David Winsemius dwinsemius at comcast.net
Fri Mar 1 23:45:51 CET 2013


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



More information about the R-help mailing list