Re: [R] Multiple counters in a single for loop

2018-08-26 Thread Duncan Murdoch
On 26/08/2018 3:10 AM, Jeremie Juste wrote: Duncan Murdoch writes: for ( i in 1:length(var1)){ This is generally a bad idea: if length(var1) == 0, it does the wrong thing, since 1:0 is c(1L, 0L). Better to use for ( i in seq_along(var1) ) { granted. One should check the validity of th

Re: [R] Multiple counters in a single for loop

2018-08-26 Thread Jérémie Juste
Of course I meant >If the length of var1 should not be 0 so stopifnot(length(var)>0) On Sun, Aug 26, 2018 at 9:10 AM, Jeremie Juste wrote: > Duncan Murdoch writes: > > >> for ( i in 1:length(var1)){ > > > > This is generally a bad idea: if length(var1) == 0, it does the wrong > > thing, sinc

Re: [R] Multiple counters in a single for loop

2018-08-26 Thread Jeremie Juste
Duncan Murdoch writes: >> for ( i in 1:length(var1)){ > > This is generally a bad idea: if length(var1) == 0, it does the wrong > thing, since 1:0 is c(1L, 0L). Better to use > > for ( i in seq_along(var1) ) { > granted. One should check the validity of their variables before using them but I

Re: [R] Multiple counters in a single for loop

2018-08-25 Thread Duncan Murdoch
On 25/08/2018 1:47 PM, jeremieju...@gmail.com wrote: Hello, I'm aware it is not the answer you are expecting but indexes are not that bad to implement as well. for ( i in 1:length(var1)){ This is generally a bad idea: if length(var1) == 0, it does the wrong thing, since 1:0 is c(1L, 0L). B

Re: [R] Multiple counters in a single for loop

2018-08-25 Thread jeremiejuste
Hello, I'm aware it is not the answer you are expecting but indexes are not that bad to implement as well. for ( i in 1:length(var1)){ elem1 <-var1[i] elem2 <- var2[i] } if you want more abstraction you could then wrap that up in a function HTHOn 25 Aug 2018 18:57, Jeff Newmiller wrote: > >

Re: [R] Multiple counters in a single for loop

2018-08-25 Thread Jeff Newmiller
look at the map2 function in the purrr package. On August 24, 2018 6:44:19 AM PDT, Deepa 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? > > [[altern

Re: [R] Multiple counters in a single for loop

2018-08-24 Thread MacQueen, Don via R-help
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

Re: [R] Multiple counters in a single for loop

2018-08-24 Thread Bert Gunter
Sort of, but you typically wouldn't need to in R because of vectorization, which buries the iteration in the underlying C code. Here's an example that may clarify what I mean: x <- cbind(1:5,6:10) x ## a 2 column matrix ## get squares of all elements of x ## method 1 m1 <-x^2 ##method 2: square t

[R] Multiple counters in a single for loop

2018-08-24 Thread Deepa
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@r-project.org mailing list