[R] How can I access the list argument within a "for" function call

Mike Rowe mwrowe at gmail.com
Mon Nov 24 03:30:52 CET 2008


I'm sorry if I wasn't clear before...

Although it is hidden syntactically, a for-loop construct is
implemented
by a call to the "for" function:

"for"(var, list, expr)

...where var is the iteration variable, list is the sequence of
values
assigned to var on successive iterations, and expr is the statement
or
statements inside the body of the loop that get executed.  For
example,
you can recast:

for(X in 1:10) print(x)

as:

"for"(x,1:10,print(x))


When "inside" other functions, I can get their arguments using sys.call
(1).
Calls to the for function do not seem to create a new frame though, so
this
doesn't work.  What I want to know is how to retrieve the list
argument
from within the loop.

Thanks!
Mike




On Nov 22, 7:03 am, "jim holtman" <jholt... at gmail.com> wrote:
> I am not sure exactly what you mean with your question.  If you have a
> named list and want to access the 'name' within the 'for' along with
> the value, then you can do something like this:
>
> for (i in names(myList)){
>     value <- myList[[i]]   # get the values
>     plot(values, main=paste("Plot of", i))
>
> }
>
> A little more clarification is need on your question.
>
>
>
> On Fri, Nov 21, 2008 at 3:30 PM, MikeRowe<mi... at tethysbio.com> wrote:
> > Greetings,
>
> > I have been playing around with the R/Parallel package, which can farm out the computation of a for-loop among multiple worker processes.  Each worker gets a chunk of the for-loop iterations; for example, if you have two workers and for(x in 1:1000){...}, one worker would typically get iterations 1:500, while the second does 501:1000.
>
> > I would like to be able to preallocate matrices to capture the results within the for loop, on the first iteration.  Is there a way to access the list argument of the "for" function programmatically from within the loop?  I am referring to the for arguments shown on page 51 of the R Language Definition manual:
>
> >   "for"(var,list,expr)
>
> > I think I have a work-around, but was curious whether this was possible...
>
> > Thanks,
> > Mike
>
> >        [[alternative HTML version deleted]]
>
> > ______________________________________________
> > R-h... at r-project.org mailing list
> >https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem that you are trying to solve?
>
> ______________________________________________
> R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list