Kunal Shetty
2004-Oct-25 18:20 UTC
[R] Ref: Variable scope or function behaviour or array reassign
Dear R- helpers Following a draft structure of the R script for which I am facing problem Step 1 x <- of type array with original values y <- of type array with original values Step 2 for (ctr in 1:10) { # my problem here the both x and y still show the original values from step 1 # in spite of making changes to the old values of the arrays x and y in the function function (x,y) ??? } step3 output < - function(parX,parY){ Variables for New X and Y newx <- array(parX, dim=c(1,length(parX))) newy <- array(parY, dim=c(1,length(parY))) # make some calculation and updated some arrays element in the newX and # #newY # finally assign the global original values x and y with newX and newY x<- newx y<- newy # if print here I can see the new values # but when the function gets called the second time the original values of # x and y get called hence failing my motive of passing update values of #the arrays to the function each time ??? } I believe there something to deal with env or new.env.... but never could get the concept of the variable scope.....where the fact with the function the x and y get updated but while calling from the main loop the x and y are pointing to old values also I am keen to know as to is there a way to clear the array of old values and reasign new values..or the <- operator take care of it by overwriting regards Kunal
Uwe Ligges
2004-Oct-25 19:29 UTC
[R] Ref: Variable scope or function behaviour or array reassign
Kunal Shetty wrote:> Dear R- helpers > > Following a draft structure of the R script for which I am facing problem > > Step 1 > x <- of type array with original values > y <- of type array with original values > > > Step 2 > > for (ctr in 1:10) { > > # my problem here the both x and y still show the original values from step 1 > # in spite of making changes to the old values of the arrays x and y in the function > function (x,y) ??? > > } > > > step3 > > > output < - function(parX,parY){ > > Variables for New X and Y > newx <- array(parX, dim=c(1,length(parX))) > newy <- array(parY, dim=c(1,length(parY))) > > # make some calculation and updated some arrays element in the newX and # #newY > > > # finally assign the global original values x and y with newX and newY > > x<- newx > y<- newy > # if print here I can see the new values > > # but when the function gets called the second time the original values of > # x and y get called hence failing my motive of passing update values of #the arrays to the function each time ??? > } > I believe there something to deal with env or new.env.... but never could get the concept of the variable scope.....where the fact with the function the x and y get updated but while calling from the main loop the x and y are pointing to old values > > also I am keen to know as to is there a way to clear the array of old values and reasign new values..or the <- operator take care of it by overwriting > > regards > Kunal > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.htmlI don't understand your question completely. Anyway, I guess you have missed the point that you can pass arguments to a function and that you can return an object (e.g. a list of other objects) from the function (please read "An Introduction to R" for more details). Assign the value returned by a function it to an object in the calling function. In most circumstances, don't think too much about accessing environments, because you don't want to do it! The mechanisms mentioned above are sufficient in most circumstances. Uwe Ligges