x=list(a=c(1,2),b=c(3,4,5)) y=list(a=c(1,2),b=c(3,4,5)) lapply(seq(along=x),function(i){cbind(x[[i]],y[[i]])}) I need to apply some function on two lists. I have to use the index to do as shown above. But I feel more natural to refer to the lists without using the index (using the syntax of something like, lapply(x,y, function(i,j){....})). I'm wondering if there is such a thing in R? Or there are some other ways better than the example at the very beginning?
On Nov 30, 2009, at 11:07 PM, Peng Yu wrote:> x=list(a=c(1,2),b=c(3,4,5)) > y=list(a=c(1,2),b=c(3,4,5)) > lapply(seq(along=x),function(i){cbind(x[[i]],y[[i]])})mapply("cbind", x, y)> > > I need to apply some function on two lists. I have to use the index to > do as shown above. But I feel more natural to refer to the lists > without using the index (using the syntax of something like, > lapply(x,y, function(i,j){....})). I'm wondering if there is such a > thing in R? Or there are some other ways better than the example at > the very beginning? > > ______________________________________________ > 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 Heritage Laboratories West Hartford, CT
why don't you try this tmp=list(X=x,Y=y) and then lapply(tmp,fun(..)) Peng Yu wrote:> > x=list(a=c(1,2),b=c(3,4,5)) > y=list(a=c(1,2),b=c(3,4,5)) > lapply(seq(along=x),function(i){cbind(x[[i]],y[[i]])}) > > > I need to apply some function on two lists. I have to use the index to > do as shown above. But I feel more natural to refer to the lists > without using the index (using the syntax of something like, > lapply(x,y, function(i,j){....})). I'm wondering if there is such a > thing in R? Or there are some other ways better than the example at > the very beginning? > > ______________________________________________ > 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. > >-- View this message in context: http://n4.nabble.com/apply-on-two-lists-tp931873p931908.html Sent from the R help mailing list archive at Nabble.com.
Seemingly Similar Threads
- go back a block of code in history
- go back a block of code in history
- Is there an variant of apply() that does not return anything?
- Is there a summary on different version of 'apply' functions? What is the meaning of the prefixes?
- How to 'apply' on multiple arguments?