[R] Count observation based on hour

arun smartpink111 at yahoo.com
Thu Feb 6 23:34:03 CET 2014


HI Map,
I am not sure what you really wanted.  Perhaps, this helps:

dat <- structure(list(Date = c("2014-01-01 00:00:00", "2014-01-02 11:00:00", 
"2014-01-02 22:00:00", "2014-01-03 03:00:00", "2014-01-01 00:00:00", 
"2014-02-02 11:00:00", "2014-02-02 22:00:00", "2014-02-03 03:00:00", 
"2014-02-01 00:00:00", "2014-03-01 11:00:00", "2014-03-02 22:00:00", 
"2014-03-03 03:00:00", "2014-03-01 00:00:00", "2014-03-01 11:00:00", 
"2014-03-02 22:00:00", "2014-01-03 03:00:00")), .Names = "Date", class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15", "16"))
dat$Date <- as.POSIXct(dat$Date)
res <- as.data.frame(table( with(dat,cut(Date,breaks=seq(min(Date),max(Date)+3600,by= '1 hour')))))
A.K.



Hi AK, 

Thanks for the tips! But it doesn't seem to work for some 
reasons.  Basically I want to do a time series analysis and I heard that
 it is actually very easy to perform hourly count, e.g. count the number
 of occurrence hourly when the variable is in date-time format like 
this: YYYY-MM-DD HH:MM:SS.   

I was thinking a 'long' way (sorry for this if it sounds stupid 
:p) by dividing my data into say 1000 24-hr (i.e. one day) sections, 
count them all them merge... Anyway, could you give me more advice on 
this please?  Thanks! 

Map 



On Wednesday, February 5, 2014 10:59 AM, arun <smartpink111 at yahoo.com> wrote:
Hi,
Try:

data.frame(table(dat$Date))
A.K.


Hi guys, I have some 20,000 observations like this: 
          Date       
2014-01-01 00:00:00 
2014-01-02 11:00:00 
2014-01-02 22:00:00 
2014-01-03 03:00:00 

I want to perform an hourly count (e.g. the frequency occur at 
each hour for a specific date) so the result would look like this: 

          Date                    Frequency 
2014-01-01 00:00:00             1 
2014-01-01 01:00:00             1 
2014-01-01 02:00:00             1 
2014-01-01 03:00:00             1 

Any suggestions?  Thanks!



More information about the R-help mailing list