Re: [R] problem accumulating array within a function over loops

From: <vincent_at_7d4.com>
Date: Wed 05 Oct 2005 - 20:10:26 EST

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



R-help@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.html Received on Wed Oct 05 20:23:39 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 18:20:00 EST