jeremiejuste m@iii@g oii gm@ii@com
2018-Aug-25 17:47 UTC
[R] Multiple counters in a single for loop
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 <jdnewmil at dcn.davis.ca.us> wrote:> > look at the map2 function in the purrr package. > > On August 24, 2018 6:44:19 AM PDT, Deepa <deepamahm.iisc at 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 at 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. > > -- > Sent from my phone. Please excuse my brevity. > > ______________________________________________ > R-help at 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.
On 25/08/2018 1:47 PM, jeremiejuste at 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). Better to use for ( i in seq_along(var1) ) { Duncan Murdoch> 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 <jdnewmil at dcn.davis.ca.us> wrote: >> >> look at the map2 function in the purrr package. >> >> On August 24, 2018 6:44:19 AM PDT, Deepa <deepamahm.iisc at 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 at 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. >> >> -- >> Sent from my phone. Please excuse my brevity. >> >> ______________________________________________ >> R-help at 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. > ______________________________________________ > R-help at 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. >
Duncan Murdoch <murdoch.duncan at gmail.com> 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 argue that seq_along does not protect you from the unexpected behaviour. If the length of var1 should not be 0 so stopifnot(length(var)==0) for ( i in 1:length(var1)){ elem1 <-var1[i] elem2 <- var2[i] }