[R] lapply - value changes as parameters to function?

Mark Knecht markknecht at gmail.com
Fri Sep 18 19:08:13 CEST 2009


Hi,
   I'm trying to get better at things like lapply but it still stumps
me. I have a function I've written, tested and debugged using
individual calls to the function, ala:

ResultList5  = DoAvgCalcs(IndexData, Lookback=5,
SampleSize=TestSamples , Iterations=TestIterations )
ResultList8  = DoAvgCalcs(IndexData, Lookback=8,
SampleSize=TestSamples , Iterations=TestIterations )
ResultList13 = DoAvgCalcs(IndexData, Lookback=13,
SampleSize=TestSamples , Iterations=TestIterations )
ResultList21 = DoAvgCalcs(IndexData, Lookback=21,
SampleSize=TestSamples , Iterations=TestIterations )

The function returns a list of numbers which I use for processing
later. I'd like to run this on a longer list (100's of values for
Lookback) so my thought was to try lapply but so far I cannot get the
darn thing right.

   Let's say I want to run the function on a string of values:

BarTestList = list(seq(5:20))

 So my thought was something like:

x = list(seq(5:20))
ResultList = lapply(x, DoAvgCalcs, IndexData, Lookback=x,
SampleSize=TestSamples , Iterations=TestIterations )

which fails down lower complaining that what it's receiving for
Lookback isn't an integer:

> x = list(seq(5:20))
> ResultList = lapply(x, DoAvgCalcs, IndexData, Lookback=x,  SampleSize=TestSamples , Iterations=TestIterations )
Error in MyLag(df$Close, Lookback) :
  (list) object cannot be coerced to type 'integer'
>

   Can someone suggest how to do this correctly?

Thanks,
Mark




More information about the R-help mailing list