Sunny Sunny Sunny Sunny - 1 month ago 28
R Question

Frequency count of two column in R

I have two columns in data frame

2010 1
2010 1
2010 2
2010 2
2010 3
2011 1
2011 2


I want to count frequency of both columns and get the result in this format

y m Freq
2010 1 2
2010 2 2
2010 3 1
2011 1 1
2011 2 1

Answer

If your data is dataframe df with columns y and m

library(plyr)
counts <- ddply(df, .(df$y, df$m), nrow)
names(counts) <- c("y", "m", "Freq")