[R] lapply - value changes as parameters to function?
Mark Knecht
markknecht at gmail.com
Fri Sep 18 22:16:02 CEST 2009
Bert,
Boy, to me that's one of those 'you have to know what it means to
know what it means' sort of things. Thanks for pointing it out though.
I appreciate it.
Cheers,
Mark
On Fri, Sep 18, 2009 at 1:09 PM, Bert Gunter <gunter.berton at gene.com> wrote:
> It **IS** stated explicitly, though perhaps not so obviously, already in
> the help file, In the "Note" section at the end it says:
>
> "... This means that the recorded call is always of the form FUN(X[[0L]],
> ...), with 0L replaced by the current integer index. ..."
>
> So you need to read more carefully ...
>
> Bert Gunter
> Genentech Nonclinical Statistics
>
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
> Behalf Of Mark Knecht
> Sent: Friday, September 18, 2009 11:55 AM
> To: jim holtman
> Cc: r-help; Phil Spector
> Subject: Re: [R] lapply - value changes as parameters to function?
>
> Thanks Jim. That did the trick.
>
> I had wondered in passing about that as all the examples in the
> ?lapply page were pretty simple and each time it was the first
> argument. However I didn't read that this was a requirement so I
> didn't go there. Is this really stated and I just cannot see it or
> possibly should some extra verbiage be added. (Heck - it's Open Source
> - guess I could do it myself and submit it to who ever manages that
> stuff!)
>
> Again, thanks!
>
> Cheers,
> Mark
>
> On Fri, Sep 18, 2009 at 11:06 AM, jim holtman <jholtman at gmail.com> wrote:
>> Change the order of the parameters in your function so that Lookback
>> is the first one. The first parameter of the lapply is what is passed
>> to the function as its first parameter. Now just have
>>
>> ResultList <- lapply(x, DoAvgCalcs, IndexData=IndexData,
>> SampleSize=TestSamples, Iteration=TestIterations)
>>
>>
>> On Fri, Sep 18, 2009 at 1:32 PM, Mark Knecht <markknecht at gmail.com> wrote:
>>> Phil,
>>> Thanks for the reply. Your suggestion is actually the one I started
>>> with (assuming I'm understanding you) but I didn't seem to even get
>>> down into my function, or the error message is from other place within
>>> my function that I haven't discovered yet:
>>>
>>>> x = seq(5:20)
>>>> ResultList = lapply(x, DoAvgCalcs, IndexData, Lookback=x,
> SampleSize=TestSamples , Iterations=TestIterations )
>>> Error in rep(NA, k) : invalid 'times' argument
>>>>
>>>
>>> I should write some fake code that gives you all the depth so we
>>> could jsut run it. If no on eelse sees the answer then I'll be back
>>> later with more code that completely runs.
>>>
>>> Thanks,
>>> Mark
>>>
>>>
>>> On Fri, Sep 18, 2009 at 10:16 AM, Phil Spector
>>> <spector at stat.berkeley.edu> wrote:
>>>> Mark -
>>>> The "l" in lapply refers to that fact that it will *return*
>>>> a list, not that it wants a list for input. You could input a list, but
>>>> then each element of the list would be one of the values you wanted
>>>> processed. So I think you want
>>>>
>>>> x = seq(5:20)
>>>> ResultList = lapply(x, DoAvgCalcs, IndexData, Lookback=x,
>>>> SampleSize=TestSamples , Iterations=TestIterations
> )
>>>>
>>>> This will return a list whose elements are the result of calling
>>>> the DoAvgCalcs function with each value contained in x. If they
>>>> were all the same length, and you wanted them simplified to a matrix,
>>>> you could use sapply (s for simplify) instead of lapply (l for list).
>>>>
>>>> - Phil Spector
>>>> Statistical Computing Facility
>>>> Department of Statistics
>>>> UC Berkeley
>>>> spector at stat.berkeley.edu
>>>>
>>>>
>>>> On Fri, 18 Sep 2009, Mark Knecht wrote:
>>>>
>>>>> 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
>>>>>
>>>>> ______________________________________________
>>>>> R-help at r-project.org mailing list
>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>> PLEASE do read the posting guide
>>>>> http://www.R-project.org/posting-guide.html
>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>>
>>>>
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>
>>
>> --
>> Jim Holtman
>> Cincinnati, OH
>> +1 513 646 9390
>>
>> What is the problem that you are trying to solve?
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
More information about the R-help
mailing list