[R] Multiple counters in a single for loop
MacQueen, Don
m@cqueen1 @end|ng |rom ||n|@gov
Fri Aug 24 18:00:13 CEST 2018
I don't know of any such option, but it's easy enough to achieve something more or less equivalent.
x <- runif(5)
for (ir in seq(nrow(myi <- cbind(x, 1:length(x))))) {
i <- myi[ir,1]
j <- myi[ir,2]
cat(i,j,'\n')
}
I consider that for() statement to be ugly and unreadable. Normally I would build the matrix myi before constructing the loop, and make other changes for clarity. But in this instance I wanted to make it a one-liner, just to more or less mimic the python.
And having now read Bert's reply, he makes a good point. For many things one might want to do with such a loop, R can do them without an explicit loop.
-Don
--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509
On 8/24/18, 6:44 AM, "R-help on behalf of Deepa" <r-help-bounces using r-project.org on behalf of deepamahm.iisc using gmail.com> wrote:
Hello,
Is there an option to include multiple counters in a single for loop in R?
For instance, in python there is
for i,j in zip(x,range(0,len(x))):
Any suggestions?
[[alternative HTML version deleted]]
______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list