[R] Running totals

Joshua Wiley jwiley.psych at gmail.com
Mon Nov 14 15:30:57 CET 2011


Glad it worked!  The one "gotcha" is that it does not handle missing
values, so for example:

> cumsum(c(1, 2, 3, NA, 4))
[1]  1  3  6 NA NA

both the NA, and everything after it become NA (missing).  If you find
yourself working with tables and frequencies and the like, you may
also like (if you have not already seen):

?table # tabulate data ?function pulls up documentation
## example
> table(c("john", "jane", "jane", "jane"))

jane john
   3    1

?xtabs # cross tabulation
## examples using the built in mtcars dataset
## (vs and am are 0/1 data)
> xtabs( ~ vs + am, data = mtcars)
   am
vs   0  1
  0 12  6
  1  7  7

You may also like the vcd (visualizing categorical data) package by
Michael Friendly:

Keeping with the mtcars data, we might want to visualize that little cross tab:

require(vcd)
pairs(xtabs(~ vs + am, data = mtcars))

Cheers,

Josh

On Mon, Nov 14, 2011 at 1:30 AM, Mark Carter <mcturra2000 at yahoo.co.uk> wrote:
>>From: Joshua Wiley <jwiley.psych at gmail.com>
>
>
>>dat$RTotal <- cumsum(dat$BAL)
>
>
>
> Wow, that's really great. I'm starting to really enjoy using R. My statistical needs are not that great, but I love the way that R handles tabular data.
>
>
> ______________________________________________
> 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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, ATS Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list