[R] Index of a Loop Variable?

Jim Java jjava at priscian.com
Wed Apr 7 15:30:49 CEST 2004


Thanks to everyone who's responded to my request for info -- the replies
have been helpful and interesting. They'll certainly help me towards
thinking more cleverly in R....

Barry: I'd like to see your looping class, but only if you haven't anything
better to do over your break than trying to put it together again. :)

 -- Jim J.

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-
> bounces at stat.math.ethz.ch] On Behalf Of Gabor Grothendieck
> Sent: Wednesday, April 07, 2004 12:30 AM
> To: r-help at stat.math.ethz.ch
> Subject: Re: [R] Index of a Loop Variable?
> 
> You could define the utility function iter:
> 
> iter <- function(x) mapply( x, seq(along=x),
>                       FUN=function(x,i)list(x=x,i=i), SIMPLIFY=F )
> 
> and use it like this to loop over seq(5,50,5) and 1:10 simultaneously:
> 
>   > for(idx in iter(seq(5,50,5))) with(idx, cat(x,i,"\n"))
>   5 1
>   10 2
>   15 3
>   20 4
>   25 5
>   30 6
>   35 7
>   40 8
>   45 9
>   50 10
> 
> Jim Java writes--
> 
>  Hi Everyone:--
> 
>  Is it possible, within a for loop not explicitly using whole-number
>  indexing, to find out the index value of the loop variable within the
>  vector or list that's being looped through?




More information about the R-help mailing list