[R] Index of a Loop Variable?

Eric Lecoutre lecoutre at stat.ucl.ac.be
Tue Apr 6 17:17:53 CEST 2004



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




More information about the R-help mailing list