Skip to content

Running totals

5 messages · Mark Carter, michael.weylandt at gmail.com (R. Michael Weylandt, Joshua Wiley

Table is not a regular data structure in R so I can't help further without a bit of clarification, but try ?cumsum and ?cbind. 

Michael
On Nov 13, 2011, at 4:18 PM, Mark Carter <mcturra2000 at yahoo.co.uk> wrote:

            
#
Hi Mark,

Take a look at ?cumsum and see if this is what you want:

## your data output via dput() (easy to copy and paste from email to R)
dat <- structure(list(ACC = c("hal", "opn", "pga", "prt", "rbs"),
BAL = c(-171245.33,
-50487.63, 213440.38, 0.18, 8292.54)), .Names = c("ACC", "BAL"
), class = "data.frame", row.names = c("1", "2", "3", "4", "5"
))

dat$RTotal <- cumsum(dat$BAL)

dat
ACC        BAL     RTotal
1 hal -171245.33 -171245.33
2 opn  -50487.63 -221732.96
3 pga  213440.38   -8292.58
4 prt       0.18   -8292.40
5 rbs    8292.54       0.14


Hope this helps,

Josh
On Sun, Nov 13, 2011 at 1:18 PM, Mark Carter <mcturra2000 at yahoo.co.uk> wrote:

  
    
#
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.
#
Glad it worked!  The one "gotcha" is that it does not handle missing
values, so for example:
[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
jane john
   3    1

?xtabs # cross tabulation
## examples using the built in mtcars dataset
## (vs and am are 0/1 data)
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: