[R] How to group a count
Deepayan Sarkar
deepayan.sarkar at gmail.com
Wed May 16 00:26:41 CEST 2007
On 5/15/07, Spilak,Jacqueline [Edm] <Jacqueline.Spilak at ec.gc.ca> wrote:
> Here is my data. I tried table but it doesn't do what I want it to do
> when it graphs. I want a count of the types (R for one graph and A for
> another) by hour grouped by year. Hope that helps.
>
> ID,YYYY,MM,DD,HH,MM,Type
> YEG,2002,01,01,01,24,A
> YEG,2002,01,01,02,40,R
> YEG,2002,01,01,05,39,R
> YEG,2002,01,01,09,17,A
I assume you have more data than that. Here's a sample use of xtabs,
which you should adapt to your example (I'm not sure if you want to
disregard MM and DD):
> foo
ID YYYY MM DD HH MM.1 Type
1 YEG 2002 1 1 1 24 A
2 YEG 2002 1 1 2 40 R
3 YEG 2002 1 1 5 39 R
4 YEG 2002 1 1 9 17 A
> as.data.frame(xtabs(~Type + YYYY + HH, foo))
Type YYYY HH Freq
1 A 2002 1 1
2 R 2002 1 0
3 A 2002 2 0
4 R 2002 2 1
5 A 2002 5 0
6 R 2002 5 1
7 A 2002 9 1
8 R 2002 9 0
(xtabs itself will produce a 3-D array, which you may or may not be
comfortable working with).
-Deepayan
>
> -----Original Message-----
> From: Deepayan Sarkar [mailto:deepayan.sarkar at gmail.com]
> Sent: May 15, 2007 3:46 PM
> To: Spilak,Jacqueline [Edm]
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] How to group a count
>
> On 5/15/07, Spilak,Jacqueline [Edm] <Jacqueline.Spilak at ec.gc.ca> wrote:
> > Hello R users
> > I have a dataset that has different types of records with different
> > dates and times pertaining to each. I would like to have a bar graph
> > of a count of the types(ie. The number of types) of recods by hour
> > grouped by year. So the count of the types would be the y axis, the
> > hour on the x axis and then grouped by year for easy comparison. I
> > think that I have to use barchart however I don't know how to get
> > barchart to do a count and not graph values.
>
> I think you want to use table or xtabs to get a frequency table, and use
> barchart on the result. Hard to say more without an example.
>
> -Deepayan
>
More information about the R-help
mailing list