On Tue, 6 Apr 2004, Jim Java wrote:> 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? For example, in -- > > x <- seq(5, 50, by=5) > index.in.x <- 1 > for (i in x) { > cat(paste(" index of i-value ", i, " within x: ", index.in.x, sep=""), fill=T) > index.in.x <- index.in.x + 1 > } > > -- is it in general possible to get values of "index.in.x" without > making it a count variable, as above?No. Sometimes it is possible to avoid the need to do this by replacing the loop with sapply(). Otherwise you probably should use for(i in seq(length=length(x)) ) and get the elements of x as x[i]. -thomas
Hi Jim,
Use the build "seq(along=vec))" as in:
x <- seq(5, 50, by=5)
for (i in seq(along=x))
cat(paste(" index of i-value ", i, " with x-value: ", x[i],
sep=""), fill=T)
Eric
At 00:55 7/04/2004, Jim Java wrote:>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? For example, in --
>
>x <- seq(5, 50, by=5)
>index.in.x <- 1
>for (i in x) {
> cat(paste(" index of i-value ", i, " within x: ",
index.in.x, sep=""),
> fill=T)
> index.in.x <- index.in.x + 1
>}
>
> -- is it in general possible to get values of "index.in.x"
without
>making it a count variable, as above?
>
>Thank you.
>
> -- Jim Java
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
Eric Lecoutre
UCL / Institut de Statistique
Voie du Roman Pays, 20
1348 Louvain-la-Neuve
Belgium
tel: (+32)(0)10473050
lecoutre at stat.ucl.ac.be
http://www.stat.ucl.ac.be/ISpersonnel/lecoutre
If the statistics are boring, then you've got the wrong numbers. -Edward
Tufte
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? For example, in --
x <- seq(5, 50, by=5)
index.in.x <- 1
for (i in x) {
cat(paste(" index of i-value ", i, " within x: ",
index.in.x, sep=""), fill=T)
index.in.x <- index.in.x + 1
}
-- is it in general possible to get values of "index.in.x" without
making it a count variable, as above?
Thank you.
-- Jim Java
On Tue, 06-Apr-2004 at 03:55PM -0700, Jim Java wrote:
|> 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
Depending on what you do with the index in the loop, it's simple
enough to use the names of a list instead of the indexes. For
example, you can use the part of the list identified by its name
without needing to know what its index is.
for(i in names(xx)){
do.something(xx[[i]], ...)
etc
}
But if it's the index you need, the answer would be No.
HTH
--
Patrick Connolly
HortResearch
Mt Albert
Auckland
New Zealand
Ph: +64-9 815 4200 x 7188
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~
I have the world`s largest collection of seashells. I keep it on all
the beaches of the world ... Perhaps you`ve seen it. ---Steven Wright
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~
>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? For example, in -- x <- seq(5, 50, by=5) index.in.x <- 1 for (i in x) { cat(paste(" index of i-value ", i, " within x: ", index.in.x, sep=""), fill=T) index.in.x <- index.in.x + 1 } -- is it in general possible to get values of "index.in.x" without making it a count variable, as above? Thank you. -- Jim Java