# [R] For Loop performance

Uwe Ligges ligges at statistik.tu-dortmund.de
Sun Jan 13 20:03:47 CET 2008

```
mcoyne at boninc.com wrote:
> WRT:  Say length(V1) is n, do you want to compare
>> v1[1] with v2[1] and v2[2] and v1[2] with v2[3] and v2[4]
>> or
>> v1[1] with v2[1] and v2[n+1] and v1[2] with v2[2] and v2[n+2]
>
> v1[1] with (v2[1] and v2[2])
> v1[2] with (v2[3] and v2[4])
> v1[3] with (v2[5] and v2[6])
> ....
> ...
> v1[n] with (v2[n+1] and v2[n+2])

So you end up with all comparisons by:

v2m <- matrix(v2, ncol=2, byrow=TRUE)
logical_result <- (v1 == v2m[,1]) & (v1 == v2m[,2])

Now you can (1) apply vectorized statements (e.g. using ifelse()) OR (if
1 is impossible) (2):

for (I in which(logical_result)) {
statement_1
statement_2
}

Uwe Ligges

> I hope this is clear (I think I was wrong in the snippet of code earlier).
>
> --MyC
>
>>
>> My Coyne wrote:
>>> Hello,
>>>
>>>
>>>
>>> Newbie question and hope you can help .
>>>
>>> I have two vector V1 and V2, where length(V2) = length of (V1) * 2;
>>> length(V1) ~ 16,000.
>>>
>>> For each member in V1, I need to compare 2 element of V2 for equality
>>
>> If just the comparison is concerned, you can do it in vectorized form,
>> but I am not sure of you want to comapre v1[i] with v2[i] and v2[i+1],
>> otherwise it would not make sense that  length(V2) = length of (V1) * 2
>> ...
>>
>> Say length(V1) is n, do you want to compare
>>
>> v1[1] with v2[1] and v2[2] and v1[2] with v2[3] and v2[4]
>> or
>> v1[1] with v2[1] and v2[n+1] and v1[2] with v2[2] and v2[n+2]
>>
>> ???
>>
>> Uwe Ligges
>>
>>
>>
>>
>>
>>
>>>
>>> i.e.
>>>
>>> for (I in 1:length (V1)) {
>>>
>>>      if ( v2[i] == v1[i] & v2[i+1]==v1[i] ){
>>>
>>>           statement_1
>>>
>>>           statement_2
>>>
>>>           .
>>>
>>>      }
>>>
>>> }
>>>
>>>
>>>
>>> This for-loop is too slow (it takes a good 5 minutes on my Windows
>>> machine)
>>> to  finish processing the vector V1 of 16,000; I will need to process a
>>> lot
>>> more than 16,000  (about 300*16,000).
>>>
>>>
>>>
>>> Is there a better way to do looping with R?  Any help is greatly
>>> appreciate
>>>
>>>
>>>
>>> --MyC.
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> 	[[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help