Skip to content
Prev 279266 / 398503 Next

Vectorization instead of loops problem

On 04.12.2011 16:18, Costas Vorlow wrote:
One way is (assuming your data is in a data.frame called dat):

  wx <- which(dat$x==1)
  result <- lapply(wx[wx > 3], function(x) dat$y[x - (1:3)])

(where lapply is a loop, implicitly).


Uwe Ligges