[R] running count in data.frame
Mark Knecht
markknecht at gmail.com
Wed Jul 1 14:44:13 CEST 2009
Yes Jim. Thanks. That's what I was looking for. My mistake letting [pos] block.
Cheers,
Mark
On Tue, Jun 30, 2009 at 8:04 PM, jim holtman<jholtman at gmail.com> wrote:
> Not exactly sure what you want to count. Does this do what you want (made a
> change in RunningCount)
>
<SNIP>
>> RunningCount = function (MyFrame) {
> + ## Running count of p & l events
> +
> + pos <- (MyFrame$p > 0)
> + MyFrame$pc <- cumsum(as.integer(pos))
> + pos <- (MyFrame$l < 0)
> + MyFrame$lc <- cumsum(as.integer(pos))
> +
<SNIP>
>> F1 <- RunningCount(F1)
>> F1
> x y p l pc lc
> 1 1 -4 0 -4 0 1
> 2 2 -3 0 -3 0 2
> 3 3 -2 0 -2 0 3
> 4 4 -1 0 -1 0 4
> 5 5 0 0 0 0 4
> 6 6 1 1 0 1 4
> 7 7 2 2 0 2 4
> 8 8 3 3 0 3 4
> 9 9 4 4 0 4 4
> 10 10 5 5 0 5 4
<SNIP>
More information about the R-help
mailing list