An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20111113/15637375/attachment.pl>
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:
I have a table which looks like this: ? ACC??????? BAL 1 hal -171245.33 2 opn? -50487.63 3 pga? 213440.38 4 prt?????? 0.18 5 rbs??? 8292.54 How do I create a column which shows the running totals of the BAL columns? [[alternative HTML version deleted]]
______________________________________________ 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.
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
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:
I have a table which looks like this: ? ACC??????? BAL 1 hal -171245.33 2 opn? -50487.63 3 pga? 213440.38 4 prt?????? 0.18 5 rbs??? 8292.54 How do I create a column which shows the running totals of the BAL columns? ? ? ? ?[[alternative HTML version deleted]]
______________________________________________ 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/
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.
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/