Hutchinson,David [PYR]
2008-Sep-23 23:10 UTC
[R] Counting character occurrences in data frame
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
Henrique Dallazuanna
2008-Sep-23 23:41 UTC
[R] Counting character occurrences in data frame
Try this: with(DF, tapply(code, list(year, month, code), length)) On Tue, Sep 23, 2008 at 8:10 PM, Hutchinson,David [PYR] <David.Hutchinson at ec.gc.ca> 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
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