[R] problem accumulating array within a function over loops

vincent@7d4.com vincent at 7d4.com
Wed Oct 5 12:10:26 CEST 2005


Jonathan Williams a écrit :

> maxrun=3; a=array(NA, c(3,5)); run=0
> 
> testf=function(x,y){
> print(paste('Run:',run)) #check that the function knows about "run"
> a[run,1:3]=runif(3); a[run,4]=x; a[run,5]=y #collect numbers into array "a"
> }

a outside testf is a "global variable".
a inside testf is a "local variable", ie a variable local to testf().
They are not the same.

As far as I remember, to assign a global variable from inside a
function you have to use the <-- operator.

(By the way, for 2dim arrays, there is also matrix().)

hih
Vincent




More information about the R-help mailing list