# [R] [External] Re: Speeding up a loop

Tue Jul 24 14:37:49 CEST 2012

```Hello,

Sorry for the mess, the logical operation should also be changed to
conjunction:

no <- x[i, 1] <= a1 & x[i, 2] <= a2 & x[i, 3] >= a3 & x[i, 4] <= a4

Em 24-07-2012 13:09, Rui Barradas escreveu:
> Hello,
>
> Are you sure? With a matirx composed of those two rows only I had a
> problem, the function to.keep() returned NULL. See the changes made to
> avoid it.
>
> # beginning of loop
>     for(i in seq_len(nrow(x))){
>         #yes <- x[i, 1] > a1 | x[i, 2] > a2 | x[i, 3] < a3 | x[i, 4] > a4
>         #if(all(yes)) keep(i, e)
>         # Original post, do NOT remove if equal
>         #no <- x[i, 1] < a1 | x[i, 2] < a2 | x[i, 3] > a3 | x[i, 4] < a4
>         # Changed to remove if equal
>         no <- x[i, 1] <= a1 | x[i, 2] <= a2 | x[i, 3] >= a3 | x[i, 4]
> <= a4
>         if(all(!no)) keep(i, e)
>     }
>     if(e\$ires == 0 && nrow(x) > 0)
>         x[1, ]
>     else
>         e\$result[seq_len(e\$ires), 1:nc]
> # end of function
>
>
> Em 23-07-2012 18:18, Reith, William [USA] escreveu:
>> It looks like both ways produce the same result.
>>
>> -----Original Message-----
>> From: Rui Barradas [mailto:ruipbarradas at sapo.pt]
>> Sent: Monday, July 23, 2012 1:05 PM
>> To: Reith, William [USA]
>> Subject: Re: [External] Re: [R] Speeding up a loop
>>
>> Hello,
>>
>> But that's the negation of '<', so try to negate '<=', meaning,
>> remove the equal signs. Sorry if I wasn't very clear.
>>
>>
>> Em 23-07-2012 17:44, Reith, William [USA] escreveu:
>>> This is what I have for the yes for loop
>>>
>>> for(i in seq_len(nrow(x))){
>>>       yes <- x[i, 1] >= a1 | x[i, 2] >= a2 | x[i, 3] <= a3 | x[i,
>>> 4]>= a4
>>>       if(all(yes)) keep(i, e)
>>>     }
>>>
>>> -----Original Message-----
>>> From: Rui Barradas [mailto:ruipbarradas at sapo.pt]
>>> Sent: Monday, July 23, 2012 12:14 PM
>>> To: Reith, William [USA]
>>> Cc: r-help
>>> Subject: [External] Re: [R] Speeding up a loop
>>>
>>> Hello,
>>>
>>> I think this is a boundary issue. In your op you've said "less" not
>>> "less than or equal to".
>>> Try using "<=" and ">=" to see what happens, I bet it solves it.
>>>
>>>
>>> Em 23-07-2012 14:43, wwreith escreveu:
>>>> 1.15           60 0.553555415         0.574892872
>>>> 1.15       60    0.563183983         0.564029359
>>>>
>>>> Shouldn't the function row out the second one, since it it higher in
>>>> position 3 and lower in position 4 i.e. it should not all be yes?
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> View this message in context:
>>>> http://r.789695.n4.nabble.com/Speeding-up-a-loop-tp4637201p4637438.ht
>>>> m l Sent from the R help mailing list archive at Nabble.com.
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> 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
> 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.

```