[R] counting subgroup sums within a data frame

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Wed Aug 26 15:16:45 CEST 2009

Have a look at the reshape package.

Assuming that your data is in a data.frame called "dataset".

cast(Date ~ ., data = dataset, value = "count", fun = sum) 
cast(Date ~ class, data = dataset, value = "count", fun = sum) 
cast(Date + class ~ ., data = dataset, value = "count", fun = sum) 

Or the plyr package

ddply(dataset, c("Date"), function(x){c(Sum_of_counts = sum(x$count))})
ddply(dataset, c("Date", "class"), function(x){c(Sum_of_counts =



ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
~ John Tukey

-----Oorspronkelijk bericht-----
Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
Namens Shaun Grannis
Verzonden: woensdag 26 augustus 2009 15:07
Aan: r-help at r-project.org
Onderwerp: [R] counting subgroup sums within a data frame


I'm sure there's an easy approach to this issue, I'm just not seeing it.

I have a data frame of the following form:

  Date     class    subclass   count
8/1/2009    A        X          1
8/1/2009    B        X          2
8/1/2009    A        Y          9
8/1/2009    B        Y          3
8/2/2009    A        X          1
8/2/2009    B        X          5
8/2/2009    A        Y          4
8/2/2009    B        Y          2
8/3/2009    A        X          6
8/3/2009    B        X          4
8/3/2009    A        Y          3
8/3/2009    B        Y          4
8/4/2009    A        X          1
8/4/2009    B        X          9
8/4/2009    A        Y          3
8/4/2009    B        Y          5
8/5/2009    A        X          3
8/5/2009    B        X          7
8/5/2009    A        Y          2
8/5/2009    B        Y          1

I would like to create a data frame of the sum the daily counts for,
say, class 'A', like so:

  Date     sum_of_counts
8/1/2009       10
8/2/2009        5
8/3/2009        9
8/4/2009        4
8/5/2009        5

I ultimately would like to do sum of counts on all classes and
subclasses.  It seems that this is equivalent to a GROUP BY query in

I'm sure this is possible in R. Any suggestions?

	[[alternative HTML version deleted]]

R-help at r-project.org mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.

Druk dit bericht a.u.b. niet onnodig af.
Please do not print this message unnecessarily.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message 
and any annex are purely those of the writer and may not be regarded as stating 
an official position of INBO, as long as the message is not confirmed by a duly 
signed document.

More information about the R-help mailing list