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) 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]]
David Winsemius
2010-May-08 14:04 UTC
[R] apply a function on elements of a list two by two
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 at Ugent.be > ------------------------------- > Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
Patrick Hausmann
2010-May-08 14:12 UTC
[R] apply a function on elements of a list two by two
Am 08.05.2010 15:43, schrieb Joris Meys:> 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)# Using 'lapply' and 'split' is a little bit more flexible: test <- lapply(split(iris[, -5], iris$Species), function(x) dist(x))> > # 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)Sorry, no answer HTH Patrick> > Cheers > Joris