[R] apply/return and reuse

Gabor Grothendieck ggrothendieck at gmail.com
Sun Jul 19 00:34:21 CEST 2009


I am not entirely clear on what you want to do but
if you simply want a cumulative sum use cumsum:

cumsum(rep(100, 5)) + 10000

or to do cumsum using Reduce and + try:

Reduce("+", rep(100, 5), init = 10000, acc = TRUE)

On Sat, Jul 18, 2009 at 3:59 PM, Mark Knecht<markknecht at gmail.com> wrote:
> Hi Gabor,
>   Thanks for the pointer to Reduce. It looks quite interesting. I
> made an attempt to use it but I'm not clear how I would move the
> output of the Reduce execution on row 1 to become the Initial value on
> Row 2. In this output:
>
>> MyDF
>   Event Initial Offset Final
> 1      1   10000    -31  9969
> 2      2       0     10    10
> 3      3       0   -133  -133
> 4      4       0    -91   -91
> 5      5       0   -145  -145
> 6      6       0     74    74
> 7      7       0      4     4
> 8      8       0     19    19
> 9      9       0   -120  -120
> 10    10       0     47    47
>>
>
> It seems that the intended use of Reduce is for when I have all the
> values previously set up in the array and then want to execute the
> same commands down through the array. That is very powerful and makes
> sense in most cases, but in my case I have to calculate the values on
> a line-by-line basis where the execution of Reduce on row 1 (the 9969)
> must become the Initial value on row 2 before Reduce it's work on row
> 2.
>
> I think I'm not fully grasping your intentions here.
>
> Code follows.
>
> Thanks,
> Mark
>
>
>
>
> InitialValue = 10000
>
> MyDF = data.frame(cbind(Event = 1:10, Initial = 0, Offset = 0 , Final = 0))
> MyDF$Offset = round(100*rnorm(10), 0)
> MyDF$Initial[1] = InitialCash
> MyDF
>
> AddPL = function(x) Reduce("+", x[2:3])
>
> MyDF$Final = AddPL(MyDF)
>
> MyDF
>
>
>
> On Fri, Jul 17, 2009 at 9:02 PM, Gabor
> Grothendieck<ggrothendieck at gmail.com> wrote:
>> See ?Reduce
>>
>> On Fri, Jul 17, 2009 at 11:10 PM, Mark Knecht<markknecht at gmail.com> wrote:
>>> Hi,
>>>   Is it possible to make something like the following code actually
>>> work? My goal in this example would be that I'd see results like
>>>
>>> 1   10000   10100
>>> 2   10100   10200
>>> 3   10200   10300
>>> 4   10300   10400
>>>
>>> In real usage the function would obviously do a lot more work, but the
>>> question I cannot answer myself yet is whether the apply can return a
>>> value from the work on one row and then use that value as the input to
>>> the function for the next row?
>>>
>>> Thanks,
>>> Mark
>>>
>>>
>>> ReturnLast = function (.row, NextInitial=100) {
>>>   .row$Initial = as.numeric(NextInitial)
>>>   .row$Final = as.numeric(.row$Initial+100)
>>> }
>>>
>>> MyStart = 10000
>>> X = data.frame(cbind(Event = 1:10, Initial = 0, Final = 0))
>>>
>>> X
>>>
>>> MyStart  = apply(X, 1, ReturnLast( X, MyStart))
>>>
>>> X
>>>
>>> ______________________________________________
>>> 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