[R] Frequency of Data

Gabor Grothendieck ggrothendieck at myway.com
Wed Feb 2 22:08:18 CET 2005


Carsten Steinhoff <carsten.steinhoff <at> stud.uni-goettingen.de> writes:

: 
: Hello,
: 
: just another problem in R, maybe it's simple to solve for you. I didn't find
: a solution up to now, but I'm convinced that I'm not the only one who
: has/had a similar problem. Maybe there's a ready-made function in R?
: 
: The prob:
: 
: I've imported a CSV-file into R with 1000 dates of an observed event
: (there's only information of the date. When there happend no event the date
: is not recorded, when there have been two events it's recordet twice). Now I
: want to COUNT the frequency of events in every month or year.
: 
: The CSV-data is structured as:
: 
: date
: 25.02.2003
: 29.07.1997
: ...
: 
: My desired output would be a matrix with n rows for the years and m columns
: for the month.
: 
: How could a solution look like ? If the format is no matrix it doesn't
: matter. Importend is the extraction of frequency from my data.


Assuming that dd is a vector of class Date, create vectors yy and mm 
with the years and months as factors and then use table:

yy <- as.numeric(format(dd, "%Y"))
yy <- factor(yy, levels = seq(min(yy), max(yy)))
mm <- factor(as.numeric(format(dd, "%m")), levels = 1:12)
table(yy,mm)




More information about the R-help mailing list