[R] Frequency Counts per Time Period

jason devjason at gmail.com
Fri Apr 10 20:43:32 CEST 2009


Hello all,

I would like to count the number of events that occur per minute for
some measurements I have taken.  I would eventually like to look at
the sums of some of the measurements for the grouped events.  I think
I am not understanding how to use the returned value from the trunc
function correctly, or I am having some sort of fundamental confusion
about the POSIX classes or how to use R to best accomplish this.  I
was planning to try and create a factor from the truncated times then
use table().  I'm open to any advice or recommendations about packages
I can use.

What I have looks something like:
          event.times event.values
1 2009-04-10 12:00:01    1.0378239
2 2009-04-10 12:00:02    0.1466919
3 2009-04-10 12:00:03   -1.5960907
4 2009-04-10 12:01:01   -0.1722599
5 2009-04-10 12:01:02    0.1030427
6 2009-04-10 12:02:02    0.5252564
7 2009-04-10 12:02:15    0.4551200
8 2009-04-10 12:03:20   -1.4248654


What I would like is something like:
                                Count
2009-04-10 12:00:00                 3
2009-04-10 12:01:00                 2
2009-04-10 12:02:00                 2
2009-04-10 12:03:00                 1


Here is some sample code of where I am at.

###################################################
# Create a mock of my data frame
start.time <- as.POSIXct("2009-04-10 12:00:00")
event.times <- start.time + c(1,2,3,61,62,122,135,200)
event.values <- rnorm(length(event.times))
et <- data.frame(event.times,event.values)

# This seems to give me part of what I want, the event times truncated
to the minute
trunc(event.times, units="mins")

# However, this line gives an error of "replacement has 9 rows, data has 8"
et$trunc.times <- trunc(event.times, units="mins")
###################################################

I'm also confused why this works as I would expect:
> factor(event.times)

However, the following fails with the error 'x' must be atomic for 'sort.list'
> factor(trunc(event.times,units="mins"))


Thanks in advance,
Jason


> sessionInfo()
R version 2.8.1 (2008-12-22)
x86_64-pc-linux-gnu

locale:
LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=C;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base




More information about the R-help mailing list