[R] Using lapply when there are dependencies

blue honour bluehonour at yahoo.com
Thu May 28 01:34:01 CEST 2015


Hi all,

Let's say I have a vector:

vv<-c(1,2,3)


And suppose I have a function f(a,b), which is a function of 2 scalar inputs. I would like to evaluate this function separately for each element of the vv vector while the second input to f( ) will be the previous output from f( ). So, the valuation of f() has a dependency on the previous f( ) valuation (recursive). This type of calculation is easy to set up with a for loop but that will run slow. How can I achieve this with apply family of functions please?

I have the same question for the case when vv is a data.table instead of a vector.

Thank you for any help.



More information about the R-help mailing list