Skip to content
Prev 361573 / 398502 Next

apply and cousins

Hopefully Bert and William won't be offended if I more or less summarize:

Are you assuming a loop will take ages, or have you actually tested it? I
wouldn't assume a loop will take ages, or that it will take much longer
than apply().

What's wrong with

  apply( X[ {logical expression } , ] , 1, function {do something} )

?

Where the logical expression identifies (by row index or any other method)
which rows you need to work on. I would expect it to be faster to subset
the rows first, rather than test for inclusion at every iteration within a
loop.

Also, if the data is acquired in such a way that you can know that the
most recent set of measurements is the last n rows, then tail(X,n) might
be good. For example,
[,1] [,2]
 [1,]    1   11
 [2,]    2   12
 [3,]    3   13
 [4,]    4   14
 [5,]    5   15
 [6,]    6   16
 [7,]    7   17
 [8,]    8   18
 [9,]    9   19
[10,]   10   20
[,1] [,2]
 [7,]    7   17
 [8,]    8   18
 [9,]    9   19
[10,]   10   20