[R] apply/return and reuse

Gabor Grothendieck ggrothendieck at gmail.com
Sat Jul 18 06:02:44 CEST 2009


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