[R] Using lapply when there are dependencies

blue honour bluehonour at yahoo.com
Thu May 28 08:42:50 CEST 2015


Thank you. May I ask, what would you recommend reading in order to learn how to vectorise loops with dependencies using data.table facilities? I have been searching online on this topic since last night but no luck yet. 


--------------------------------------------
On Thu, 5/28/15, David Winsemius <dwinsemius at comcast.net> wrote:

 Subject: Re: [R] Using lapply when there are dependencies

 Cc: r-help at r-project.org
 Date: Thursday, May 28, 2015, 7:02 AM
 
 
 On May
 27, 2015, at 4:34 PM, blue honour via R-help wrote:
 
 > 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?
 
 The speed
 of loops is determined by the speed of their inner
 functions. The Reduce() function does what you request but
 for the application described it will need an init value.
 
 Reduce(f, vv,
 init=<something>)
 
 > I
 have the same question for the case when vv is a data.table
 instead of a vector.
 
 I could be wrong but that doesn't sound
 like an effective use of the data.table facilities.
 
 -- 
 
 David Winsemius
 Alameda, CA,
 USA



More information about the R-help mailing list