Multiple counters in a single for loop

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Multiple counters in a single for loop

Deepa Maheshvare
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Multiple counters in a single for loop

Bert Gunter-2
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 the column vectors
m2 <- x
for (i in 1:2)m2[,i] <- m2[,i]^2
identical(m1,m2)
## of course, one could do this by row vectors, too

## method 3: loop through each element
m3 <- x
ix <- as.matrix(expand.grid(1:5,1:2))
ix
m3[ix]^2 ## matrix indexing of an array. This produces a vector,though.

Note also that there is an "iterators" package in R which implements
python-like iterators.I don't know how efficient it is, however.

My overall advice would be that you should try to program in R's native
paradigms, which emphasize whole object manipulation through vectorization,
rather than trying to use Python's, especially if efficiency is a
consideration. Feel free to ignore of course.

Cheers,
Bert

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Fri, Aug 24, 2018 at 6:44 AM Deepa <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Multiple counters in a single for loop

R help mailing list-2
In reply to this post by Deepa Maheshvare
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" <[hidden email] on behalf of [hidden email]> 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]]
   
    ______________________________________________
    [hidden email] 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.
   

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Multiple counters in a single for loop

Jeff Newmiller
In reply to this post by Deepa Maheshvare
look at the map2 function in the purrr package.

On August 24, 2018 6:44:19 AM PDT, Deepa <[hidden email]> 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]]
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Multiple counters in a single for loop

Jeremie Juste
In reply to this post by Deepa Maheshvare
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 <[hidden email]> wrote:

>
> look at the map2 function in the purrr package.
>
> On August 24, 2018 6:44:19 AM PDT, Deepa <[hidden email]> 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]]
> >
> >______________________________________________
> >[hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Multiple counters in a single for loop

Duncan Murdoch-2
On 25/08/2018 1:47 PM, [hidden email] 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 <[hidden email]> wrote:
>>
>> look at the map2 function in the purrr package.
>>
>> On August 24, 2018 6:44:19 AM PDT, Deepa <[hidden email]> 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]]
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>
>> ______________________________________________
>> [hidden email] 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.
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Multiple counters in a single for loop

Jeremie Juste
Duncan Murdoch <[hidden email]> 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]

}

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Multiple counters in a single for loop

Jeremie 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 <[hidden email]>
wrote:

> Duncan Murdoch <[hidden email]> 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]
>
> }
>



--
Jérémie Juste

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Multiple counters in a single for loop

Duncan Murdoch-2
In reply to this post by Jeremie Juste
On 26/08/2018 3:10 AM, Jeremie Juste wrote:

> Duncan Murdoch <[hidden email]> 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.

I don't see why you argue that.  seq_along(var1) will give an empty
vector if var1 is empty, so the loop won't run at all.

>
> 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]
>
> }

That's a possibility (I made your >0 correction), but maybe having
length 0 isn't something that should trigger a fatal error, maybe it's
just that no elements met some condition.

Duncan Murdoch

______________________________________________
[hidden email] 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.