[R] Vectorizing a function that needs to access previous elements of a row

2010-11-20 Thread Amol Shelat
Hi, I'm a newbie when it comes to R, and I'm trying to figure out how to use vectorization as opposed to for loops. In particular, how can I create a function that is applied on each element of a row, but can access previous elements relative to that element? My problem: I want to calculate

Re: [R] Vectorizing a function that needs to access previous elements of a row

2010-11-20 Thread Joshua Wiley
Hi Amol, It depends on your exact needs, but one way, assuming you do not need to access previous calculations, only previous elements: x - 1:10 x[-10] / x[-1] The idea is first create a vector, x, then using negative indices, select the first 9 elements of x (i.e., 1:9) to be divded by the