Maiasaura
Maiasaura

Reputation: 32996

How do I summarise data using reshape/plyr?

I have data that looks like this:

                   model                aspect cover contour
1                        flowering ~ 1      2    52    2400
2   flowering ~ 1 + temp + precip:temp      1    52    2390
3        flowering ~ 1 + temp + precip      1    52    2390
4        flowering ~ 1 + temp + precip      1    52    2390
5 flowering ~ 1 + precip + precip:temp      1    52    2400
6 flowering ~ 1 + precip + precip:temp      1    52    2400

There are 40,000 rows in this dataset with 54 unique models.

How do I get a summarized dataset showing how many times each model occurs in a aspect x contour x cover combination?

Upvotes: 2

Views: 709

Answers (2)

hadley
hadley

Reputation: 103938

plyr provides an optimised function for this special case:

comboCount <- count(myData, c("model","aspect","cover","contour"))

Upvotes: 11

JD Long
JD Long

Reputation: 60756

try this (assumes your data is in a data frame called myData):

comboCount <- ddply(myData, c("model","aspect","cover","contour") function(df) nrow(df))

Upvotes: 3

Related Questions