[R] hopefully my last question on lapply

From: <markleeds_at_verizon.net>
Date: Wed 28 Jun 2006 - 12:12:57 EST

Marc and many other people ( whose names escape me ) have been very helpful in explaining the use of lapply to me.

In his last response, Marc explained that if tradevectors is a list of vectors of different lengths ( excuse my terminology ) then

lapply(tradevectors,function(x) G[x]*B[x] )

will go through each component of the list as if it was a vector and apply the element by element multiplication of G and B, ( G and B are vectors ) Then, it again returns a list of vectors.

This is fine and I understand it.

What I find confusing is that sometimes I want to take the two vectors G abd B and send them totally into some function with the respective indices from tradevectors not just the element by element index. For example, suppose I have a function called myfunction that takes two vectors as its inputs. Then, here are my ideas for what could be done.

Attempt 1 : lapply(tradevectors,function(i) myfunction(G[i],B[i])

Attempt 2 : lapply(along=tradevectors),function(i) myfunction(G[[i]],B[[i]]

In attempt1, I am just putting tradevectors and indexing using the [] which I think won't work. In attemp t2, I am using along=tradevectors and using [[]].

I think # 2 is correct but could someone confirm this because I have quite large vectors and it's not easy at all for me to check what's going on and I'm not so clear with lapply usage. There's seem tobe many different ways of setting up the first parameter in the call to lapply.


R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Jun 28 12:20:04 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 28 Jun 2006 - 18:12:34 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.