[R] Counting things in a time series.

jim holtman jholtman at gmail.com
Sun Nov 21 12:40:58 CET 2010


You never did show us what your data looks like.  You could convert
them to POSIXct, then use 'cut' to split them into the bins and then
'table' to count them.

Try something like this:

> timeStamp <- as.POSIXct('2010-11-21 00:00') + runif(50, 0, 86400)  # day's worth of time
> # bin into 1 hour buckets starting at midnight
> bins <- cut(timeStamp, breaks = seq(as.POSIXct('2010-11-21 00:00'),
+                             by = '1 hour', length = 25))
>
> table(bins)
bins
2010-11-21 00:00:00 2010-11-21 01:00:00 2010-11-21 02:00:00 2010-11-21
03:00:00 2010-11-21 04:00:00
                  1                   5                   4
       5                   5
2010-11-21 05:00:00 2010-11-21 06:00:00 2010-11-21 07:00:00 2010-11-21
08:00:00 2010-11-21 09:00:00
                  2                   2                   4
       1                   3
2010-11-21 10:00:00 2010-11-21 11:00:00 2010-11-21 12:00:00 2010-11-21
13:00:00 2010-11-21 14:00:00
                  0                   3                   3
       0                   1
2010-11-21 15:00:00 2010-11-21 16:00:00 2010-11-21 17:00:00 2010-11-21
18:00:00 2010-11-21 19:00:00
                  0                   3                   0
       3                   1
2010-11-21 20:00:00 2010-11-21 21:00:00 2010-11-21 22:00:00 2010-11-21 23:00:00
                  0                   1                   1                   2
>


On Sun, Nov 21, 2010 at 12:29 AM, Noah Silverman
<noah at smartmediacorp.com> wrote:
> Hi,
>
> I have a process (not in R) that records events with a time stamp.  So,
> I have a huge series of maybe 100,000 time stamps.
>
> I'd like to break it up into hourly (Or daily) intervals and then count
> how many events occurred in each interval.  That way I can graph it.
>
> Ideally, converting the this into a time series in R would let me do
> some interesting analysis.
>
> The data is just a list of epoch timestamps.  Importing into R is
> trivial but I'm stuck from there.
>
> 1) How can I "bin" the counts by an hour?  One thought would be to
> divided each timestamp by 3600, them multiply back by 1000.  This would
> effectively convert them to hours with multiple entries per hour. )
> But, I then don't know how to count them
>
> 2) Once I figure out the counts, I'll then have a data structure with a
> column of epoch seconds and a second column of counts.  How can I then
> convert that into a ts object?
>
> Thanks!
>
> -N
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list