r - Count number of rows within each group -


i have dataframe , count number of rows within each group. reguarly use aggregate function sum data follows:

df2 <- aggregate(x ~ year + month, data = df1, sum) 

now, count observations can't seem find proper argument fun. intuitively, thought 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)) 

there df2 <- count(x, c('year','month')) (plyr package)


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -