[R] hopefully my last question on lapply

Marc Schwartz MSchwartz at mn.rr.com
Wed Jun 28 05:46:49 CEST 2006


On Tue, 2006-06-27 at 21:12 -0500, markleeds at verizon.net wrote:
> 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. 

Mark,

My presumption is that you want to pass the subsetted vectors G and B to
myfunction() during each iteration along the list 'tradevectors' and
then perform some more complex operation on them.  

Is that correct?

If so, then I think that you are misunderstanding what is happening in
lapply(), when you use:

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

In this case, it is not the individual elements of G and B that are
passed to myfunction() one pair at a time, but the entire subsetted G[i]
and B[i] that are passed. The vector subsetting takes place BEFORE the
vectors are passed to myfunction().

Thus, for example, let's use the original data that we have been working
with here:

tempa <- c(4, 6, 10)

tempb <- c(11, 23, 39)

tradevectors <- mapply(seq, from = tempa, to = tempb)

X <- seq(2, 80, 2)
Y <- 1:40

# myfunction() will take x and y, cbind() them,
# print the result and then return a NULL to lapply
# So for each iteration in lapply, the cbind/print will be 
# executed once.
myfunction <- function(x, y) 
{
  print(cbind(x, y))
  NULL
}


> lapply(tradevectors,function(i) myfunction(X[i], Y[i]))
      x  y
[1,]  8  4
[2,] 10  5
[3,] 12  6
[4,] 14  7
[5,] 16  8
[6,] 18  9
[7,] 20 10
[8,] 22 11
       x  y
 [1,] 12  6
 [2,] 14  7
 [3,] 16  8
 [4,] 18  9
 [5,] 20 10
 [6,] 22 11
 [7,] 24 12
 [8,] 26 13
 [9,] 28 14
[10,] 30 15
[11,] 32 16
[12,] 34 17
[13,] 36 18
[14,] 38 19
[15,] 40 20
[16,] 42 21
[17,] 44 22
[18,] 46 23
       x  y
 [1,] 20 10
 [2,] 22 11
 [3,] 24 12
 [4,] 26 13
 [5,] 28 14
 [6,] 30 15
 [7,] 32 16
 [8,] 34 17
 [9,] 36 18
[10,] 38 19
[11,] 40 20
[12,] 42 21
[13,] 44 22
[14,] 46 23
[15,] 48 24
[16,] 50 25
[17,] 52 26
[18,] 54 27
[19,] 56 28
[20,] 58 29
[21,] 60 30
[22,] 62 31
[23,] 64 32
[24,] 66 33
[25,] 68 34
[26,] 70 35
[27,] 72 36
[28,] 74 37
[29,] 76 38
[30,] 78 39
[[1]]
NULL

[[2]]
NULL

[[3]]
NULL


Note that for each of the three iterations through tradevectors in
lapply(), myfunction() prints out a multi-row matrix consisting of the
subsetted vectors X and Y, using the indices provided in tradevectors.

The three final NULLs are then returned once lapply() has finished.


In addition, note that this behavior is different than mapply(), where
each argument in mapply() is passed in an element-by-element fashion to
the function indicated as the first argument. That is the behavior that
we are using above to create the sequences that become tradevectors. In
that case, each element of tempa and tempb, in sequence, are passed one
at a time as a single pair to seq(). That occurs three times.

HTH,

Marc Schwartz



More information about the R-help mailing list