[R] apply/return and reuse

Mark Knecht markknecht at gmail.com
Sat Jul 18 05:10:19 CEST 2009


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




More information about the R-help mailing list