Hi R Users, I'm wondering how can I calculate two (or three) way sum of a variable. A sample data is: State Month Year Value NC Jan 1996 1 NC Jan 1996 2 NC Feb 1997 2 NC Feb 1997 3 NC Mar 1998 3 NC Mar 1998 4 NY Jan 1996 4 NY Jan 1996 5 NY Feb 1997 5 NY Feb 1997 6 NY Mar 1998 6 NY Mar 1998 7 I'm trying to sum up "value" column by State*Month and by State*Month*Year. Also, I may need to calculate mean value along with "sum". Any help would be greatly appreciated, Thanks, Peng [[alternative HTML version deleted]]
?tapply -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Peng Cai > Sent: Thursday, December 03, 2009 11:50 AM > To: r-help at r-project.org > Subject: [R] Two-way/Three-way sum. > > Hi R Users, > > I'm wondering how can I calculate two (or three) way sum of a variable. > A > sample data is: > > State Month Year Value > NC Jan 1996 1 > NC Jan 1996 2 > NC Feb 1997 2 > NC Feb 1997 3 > NC Mar 1998 3 > NC Mar 1998 4 > NY Jan 1996 4 > NY Jan 1996 5 > NY Feb 1997 5 > NY Feb 1997 6 > NY Mar 1998 6 > NY Mar 1998 7 > > I'm trying to sum up "value" column by State*Month and by > State*Month*Year. > Also, I may need to calculate mean value along with "sum". > > Any help would be greatly appreciated, > > Thanks, > Peng > > [[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 Peng, Here is a suggestion using tapply: R> with(x, tapply(Value, list(State, Month), FUN = sum)) R> with(x, tapply(Value, list(State, Year), FUN = sum)) R> with(x, tapply(Value, list(State, Year, Month), FUN = sum)) with 'x' your data set. Please take a look at ?tapply for more information. Another useful functions might be ?ave and ?aggregate. Best, Jorge On Thu, Dec 3, 2009 at 1:50 PM, Peng Cai <> wrote:> Hi R Users, > > I'm wondering how can I calculate two (or three) way sum of a variable. A > sample data is: > > State Month Year Value > NC Jan 1996 1 > NC Jan 1996 2 > NC Feb 1997 2 > NC Feb 1997 3 > NC Mar 1998 3 > NC Mar 1998 4 > NY Jan 1996 4 > NY Jan 1996 5 > NY Feb 1997 5 > NY Feb 1997 6 > NY Mar 1998 6 > NY Mar 1998 7 > > I'm trying to sum up "value" column by State*Month and by State*Month*Year. > Also, I may need to calculate mean value along with "sum". > > Any help would be greatly appreciated, > > Thanks, > Peng > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
try this:> xState Month Year Value 1 NC Jan 1996 1 2 NC Jan 1996 2 3 NC Feb 1997 2 4 NC Feb 1997 3 5 NC Mar 1998 3 6 NC Mar 1998 4 7 NY Jan 1996 4 8 NY Jan 1996 5 9 NY Feb 1997 5 10 NY Feb 1997 6 11 NY Mar 1998 6 12 NY Mar 1998 7> tapply(x$Value, list(x$State, x$Year), sum)1996 1997 1998 NC 3 5 7 NY 9 11 13> > tapply(x$Value, list(x$State, x$Year, x$Month), sum), , Feb 1996 1997 1998 NC NA 5 NA NY NA 11 NA , , Jan 1996 1997 1998 NC 3 NA NA NY 9 NA NA , , Mar 1996 1997 1998 NC NA NA 7 NY NA NA 13>On Thu, Dec 3, 2009 at 1:50 PM, Peng Cai <pengcaimaillist@gmail.com> wrote:> Hi R Users, > > I'm wondering how can I calculate two (or three) way sum of a variable. A > sample data is: > > State Month Year Value > NC Jan 1996 1 > NC Jan 1996 2 > NC Feb 1997 2 > NC Feb 1997 3 > NC Mar 1998 3 > NC Mar 1998 4 > NY Jan 1996 4 > NY Jan 1996 5 > NY Feb 1997 5 > NY Feb 1997 6 > NY Mar 1998 6 > NY Mar 1998 7 > > I'm trying to sum up "value" column by State*Month and by State*Month*Year. > Also, I may need to calculate mean value along with "sum". > > Any help would be greatly appreciated, > > Thanks, > Peng > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
On Dec 3, 2009, at 1:50 PM, Peng Cai wrote:> Hi R Users, > > I'm wondering how can I calculate two (or three) way sum of a > variable. A > sample data is: > > State Month Year Value > NC Jan 1996 1 > NC Jan 1996 2 > NC Feb 1997 2 > NC Feb 1997 3 > NC Mar 1998 3 > NC Mar 1998 4 > NY Jan 1996 4 > NY Jan 1996 5 > NY Feb 1997 5 > NY Feb 1997 6 > NY Mar 1998 6 > NY Mar 1998 7 > > I'm trying to sum up "value" column by State*Month and by > State*Month*Year.?tapply as in sum.tbl <- with(dftbl, tapply(Value, list(State, Month), sum) )> Also, I may need to calculate mean value along with "sum".Use the mean function in the above formulation to get means, length to get counts, or perhaps summary .... or one of the several packages that offer a "describe" function. So many functions, so little time. -- David> > Any help would be greatly appreciated, > > Thanks, > Peng > > [[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.David Winsemius, MD Heritage Laboratories West Hartford, CT