I have a dataframe and I would like to count the number of rows within each group. I reguarly use the aggregate
function to sum data as follows:
df2 <- aggregate(x ~ Year + Month, data = df1, sum)
Now, I would like to count observations but can't seem to find the proper argument for FUN
. Intuitively, I thought it would be as follows:
df2 <- aggregate(x ~ Year + Month, data = df1, count)
But, no such luck.
Any ideas?
Some toy data:
set.seed(2)
df1 <- data.frame(x = 1:20,
Year = sample(2012:2014, 20, replace = TRUE),
Month = sample(month.abb[1:3], 20, replace = TRUE))
Create a new variable
Count
with a value of 1 for each row:Then aggregate dataframe, summing by the
Count
column:We can also use
dplyr
.First, some data:
Now the count:
We can also use a slightly longer version with piping and the
n()
function:or the
tally
function:There is also
df2 <- count(x, c('Year','Month'))
(plyr package)For my aggregations I usually end up wanting to see mean and "how big is this group" (a.k.a. length). So this is my handy snippet for those occasions;
You can use
by
functions asby(df1$Year, df1$Month, count)
that will produce a list of needed aggregation.The output will look like,
An old question without a
data.table
solution. So here goes...Using
.N