[R] Frequency of Data

Gabor Grothendieck ggrothendieck at myway.com
Thu Feb 3 03:58:41 CET 2005


Gabor Grothendieck <ggrothendieck <at> myway.com> writes:

: 
: 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)
: 

There is also a particularly simple solution if you assume ddc is a vector
of chron dates, e.g. ddc <- chron(dd) where dd is as above.

library(chron)
table(years(ddc), months(ddc))

However, note that it is not 100% equivalent to the prior solution because 
it omits any years that are not included at all whereas the all Date
solution included them.




More information about the R-help mailing list