Thx, works like a charm. And indeed, I didn't realize combn could be that
powerful. Thx for pointing that out.

@ Patrick : thanks for the tip. It was just a fast coded toy example, but it
never hurts to get reminded about the more efficient way, even with toy
examples.

Cheers
Joris

On Sat, May 8, 2010 at 4:04 PM, David Winsemius <dwinsemius@comcast.net>wrote:

>
> On May 8, 2010, at 9:43 AM, Joris Meys wrote:
>
>  Dear all,
>>
>> I want to apply a function to list elements, two by two. I hoped that
>> combn
>> would help me out, but I can't get it to work. A nested for-loop works,
>> but
>> seems highly inefficient when you have large lists. Is there a more
>> efficient way of approaching this?
>>
>> # Make some toy data
>> data(iris)
>> test <- vector("list",3)
>> for (i in 1:3){
>>   x <- levels(iris$Species)[i]
>>   tmp <- dist(iris[iris$Species==x,-5])
>>   test[[i]] <- tmp
>> }
>> names(test) <- levels(iris$Species)
>>
>> # nested for loop works
>> for(i in 1:2){
>>   for(j in (i+1):3){
>>       print(all.equal(test[[i]],test[[j]]))
>>   }
>> }
>>
>> # combn doesn't work
>> combn(test,2,all.equal)
>>
>
> all.equal takes two arguments:
>
> > all.equal(c(1,2))
> Error in mode(current) :
>  element 1 is empty;
>   the part of the args list of 'is.expression' being evaluated was:
>   (x)
>
> So...:
>
> > combn(test,2, function(x) all.equal(x[[1]], x[[2]]))
>     [,1]
> [1,] "Attributes: < Component 4: 50 string mismatches >"
> [2,] "Mean relative difference: 0.7888781"
>     [,2]
> [1,] "Attributes: < Component 4: 50 string mismatches >"
> [2,] "Mean relative difference: 0.953595"
>     [,3]
> [1,] "Attributes: < Component 4: 50 string mismatches >"
> [2,] "Mean relative difference: 0.6366219"
>
> Thanks for posing this problem. It made me look at portions of combn's
> capacities about which I was completely unaware/
> --
> David.
>
>>
>> Cheers
>> Joris
>> --
>> Joris Meys
>> Statistical Consultant
>>
>> Ghent University
>> Faculty of Bioscience Engineering
>> Department of Applied mathematics, biometrics and process control
>>
>> Coupure Links 653
>> B-9000 Gent
>>
>> tel : +32 9 264 59 87
>> Joris.Meys@Ugent.be
>> -------------------------------
>> Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
>>
>>        [[alternative HTML version deleted]]
>>
>>
>> ______________________________________________
>> R-help@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.
>>
>
> David Winsemius, MD
> West Hartford, CT
>
>


-- 
Joris Meys
Statistical Consultant

Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control

Coupure Links 653
B-9000 Gent

tel : +32 9 264 59 87
Joris.Meys@Ugent.be
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php

	[[alternative HTML version deleted]]

