[R] hopefully my last question on lapply

markleeds at verizon.net markleeds at verizon.net
Wed Jun 28 04:12:57 CEST 2006

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. 


More information about the R-help mailing list