Hi, Forgive a very basic question... I need to take two lists-of-lists, and apply a function to each pair of elements in the lists to return a single list... For example l1 <- list(1:5,6:10,2:15) l2 <- list(1:8,4:12,1:19,4:20) I could easily do an lapply across each of them, but is there a function that does a sort-of pairwise-apply across both together? Does anybody know of a good document that discusses this kind of basic matrix/list/vector manipulation in R? Regards, crispin -------------------------------------------------------- This email is confidential and intended solely for the use o...{{dropped}}
Crispin - This is a familiar problem. The only way I know of to do it is: result <- lapply(seq(along=list.a), function(i,a,b) do.it(a[[i]], b[[i]]), list.a, list.b) Here, do.it() is the function which operates on two elements. I use this construction frequently. - tom blackwell - u michigan medical school - ann arbor - On Mon, 6 Oct 2003, Crispin Miller wrote:> I need to take two lists-of-lists, and apply a function to each > pair of elements in the lists to return a single list... > For example > > l1 <- list(1:5,6:10,2:15) > l2 <- list(1:8,4:12,1:19,4:20) > > I could easily do an lapply across each of them, but is there a > function that does a sort-of pairwise-apply across both together? > > Does anybody know of a good document that discusses this kind of > basic matrix/list/vector manipulation in R? > Regards, > crispin
Smart! Thanks Crispin> -----Original Message----- > From: Berwin Turlach [mailto:berwin at maths.uwa.edu.au] > Sent: 06 October 2003 13:11 > To: Crispin Miller > Subject: Re: [R] Apply and its friends > > > >>>>> "CM" == Crispin Miller <CMiller at picr.man.ac.uk> writes: > > CM> Hi, > CM> Forgive a very basic question... > CM> I need to take two lists-of-lists, and apply a function to > CM> each pair of elements in the lists to return a single > CM> list... > CM> For example > > CM> l1 <- list(1:5,6:10,2:15) > CM> l2 <- list(1:8,4:12,1:19,4:20) > > CM> I could easily do an lapply across each of them, but is there > CM> a function that does a sort-of pairwise-apply across both > CM> together? > Not sure how you can do this in your case since one list has 3 > components and the other 4. :) > > But something like this should work: > > n <- min( length(l1), length(l2) ) > res <- sapply(1:n, function(x) sum(l1[[x]]) - sum(l2[[x]]) ) > > The first line calculates the minimum length of the two list. The > second one calls sapply with a function. The first argument is > essentially a vector with the components that we would like to use. > In this case I take the difference of the sum as an example. > > If you don't like to make use of global variables, you can pass on the > list on which you want to operate explicitly to the function that you > call in sapply: > > res <- sapply(1:n, function(x, d1, d2) sum(d1[[x]]) - > sum(d2[[x]]), d1=l1, d2=l2) > > Hope this helps. > > Cheers, > > Berwin Turlach > > ========================== Full address ===========================> Berwin A Turlach Tel.: +61 (8) 9380 3338 > (secr) > School of Mathematics and Statistics +61 (8) 9380 3383 > (self) > The University of Western Australia FAX : +61 (8) 9380 1028 > 35 Stirling Highway > Crawley WA 6009 e-mail: berwin at maths.uwa.edu.au > Australia http://www.maths.uwa.edu.au/~berwin > >-------------------------------------------------------- This email is confidential and intended solely for the use o...{{dropped}}
As of a recent version (1.7.1?), see ?mapply Ben On Mon, 6 Oct 2003, Thomas W Blackwell wrote:> Crispin - > > This is a familiar problem. The only way I know of to do it is: > > result <- lapply(seq(along=list.a), function(i,a,b) do.it(a[[i]], b[[i]]), list.a, list.b) > > Here, do.it() is the function which operates on two elements. > I use this construction frequently. > > - tom blackwell - u michigan medical school - ann arbor - > > On Mon, 6 Oct 2003, Crispin Miller wrote: > > > I need to take two lists-of-lists, and apply a function to each > > pair of elements in the lists to return a single list... > > For example > > > > l1 <- list(1:5,6:10,2:15) > > l2 <- list(1:8,4:12,1:19,4:20) > > > > I could easily do an lapply across each of them, but is there a > > function that does a sort-of pairwise-apply across both together? > > > > Does anybody know of a good document that discusses this kind of > > basic matrix/list/vector manipulation in R? > > Regards, > > crispin > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >-- 620B Bartram Hall bolker at zoo.ufl.edu Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker Box 118525 (ph) 352-392-5697 Gainesville, FL 32611-8525 (fax) 352-392-3704
On Mon, 6 Oct 2003, Crispin Miller wrote:> Hi, Forgive a very basic question... I need to take two lists-of-lists, > and apply a function to each pair of elements in the lists to return a > single list... For example > > l1 <- list(1:5,6:10,2:15) > l2 <- list(1:8,4:12,1:19,4:20) > > I could easily do an lapply across each of them, but is there a function > that does a sort-of pairwise-apply across both together?mapply -thomas