Re: [R] apply/return and reuse

2009-07-18 Thread Gabor Grothendieck
Yes, it just makes it more readable. On Sat, Jul 18, 2009 at 9:55 PM, Mark Knecht wrote: > Yes, I sort of guess at that one and found the answer. I don't quite > really get what this 'with' command is really doing. It seems that I > could have written something like out$Final <- out$Initial+out$Of

Re: [R] apply/return and reuse

2009-07-18 Thread Mark Knecht
Yes, I sort of guess at that one and found the answer. I don't quite really get what this 'with' command is really doing. It seems that I could have written something like out$Final <- out$Initial+out$Offset. (Untested.) I guess it's primarily a way of not having to write the data.frame's name and

Re: [R] apply/return and reuse

2009-07-18 Thread Gabor Grothendieck
Regarding the Final column the last line should have been out$Final <- with(out, Initial + Offset) On Sat, Jul 18, 2009 at 8:33 PM, Mark Knecht wrote: > Very interesting. Thanks. Very concise! interesting use of cumsum. > I'll have to see if I can work that into my real code where I need to > tak

Re: [R] apply/return and reuse

2009-07-18 Thread Mark Knecht
Hi, No, it's not about cumsum specifically. It's about building up the list as the events go by. I managed to create this code but it does depend on a for loop. Notice how the Final value on each row becomes the Initial value on the next row. Basically I want to build a data.frame with 5-10

Re: [R] apply/return and reuse

2009-07-18 Thread Mark Knecht
Very interesting. Thanks. Very concise! interesting use of cumsum. I'll have to see if I can work that into my real code where I need to take a conditional cumsum. I think it will work. On my system the Final column doesn't seem quite right but that's OK. There's enough here for me to study and I

Re: [R] apply/return and reuse

2009-07-18 Thread Gabor Grothendieck
Here `out` is the same as the final value of `MyDF` in your code using cumsum() instead of a loop: set.seed(123) DF <- data.frame(cbind(Event= 1:10, Initial=0, Offset=round(100*rnorm(10), 0), Final=0 )) out <- transform(DF, Initial = 1 + c(0, head(cumsum(DF$Offset), -1))) out$Final <- with(

Re: [R] apply/return and reuse

2009-07-18 Thread Gabor Grothendieck
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)) + 1 or to do cumsum using Reduce and + try: Reduce("+", rep(100, 5), init = 1, acc = TRUE) On Sat, Jul 18, 2009 at 3:59 PM, Mark Knecht wrote: > Hi Gabor, >   Thanks fo

Re: [R] apply/return and reuse

2009-07-18 Thread Mark Knecht
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 1-31

Re: [R] apply/return and reuse

2009-07-17 Thread Gabor Grothendieck
See ?Reduce On Fri, Jul 17, 2009 at 11:10 PM, Mark Knecht 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   1   10100 > 2   10100   10200 > 3   10200   10300 > 4   10300   10400 > > In r

[R] apply/return and reuse

2009-07-17 Thread Mark Knecht
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 1 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 canno