[R] Counting character occurrences in data frame

Hutchinson,David [PYR] David.Hutchinson at ec.gc.ca
Wed Sep 24 17:04:58 CEST 2008


Thanks Charles, ftable() works perfectly. 

-----Original Message-----
From: Charles C. Berry [mailto:cberry at tajo.ucsd.edu] 
Sent: Tuesday, September 23, 2008 5:06 PM
To: Hutchinson,David [PYR]
Cc: r-help at r-project.org
Subject: Re: [R] Counting character occurrences in data frame



See

 	?ftable
 	?as.data.frame
 	?xtabs

e.g.

 	ftable( xtabs( ~code+year+month, your.df ), col.vars=1 )
 	as.data.frame( xtabs(~code+year+month, your.df ) )

HTH,

Chuck

On Tue, 23 Sep 2008, Hutchinson,David [PYR] wrote:

> Hi R-Users,
>
> I have a data frame containing year, month, day, and code columns. The
> code column is a unique character of set ('E','A','B') - I am trying
to
> determine an efficient way of summarizing the count of each of these
> codes by month and year without having to use for...loops and subsets.
> Does anyone have any suggestions?
>
> Thanks in advance,
> Dave
>
> Here is a snapshot of my data frame:
>
>     year month day code
> 8006 1935    12   1    E
> 8007 1935    12   2
> 8008 1935    12   3    E
> 8009 1935    12   4
> 8010 1935    12   5    E
> 8011 1935    12   6    E
>
> ______________________________________________
> 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.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive
Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego
92093-0901



More information about the R-help mailing list