I think the problem is that
(a) trunc(POSIXct.object) returns a POSIXlt object
instead of a POSIXct object.
(b) length(POSIXlt.object) always returns 9 (the
number of components in the object, not the number
of time entries) while length(POSIXct.object)
returns the number of time entries.
You can work around these problems by wrapping your
call to trunc(event.times) with a call to as.POSIXct().
> et$trunc.times <- as.POSIXct(trunc(event.times, units="mins"))
> et
event.times event.values trunc.times
1 2009-04-10 12:00:01 -1.7014820 2009-04-10 12:00:00
2 2009-04-10 12:00:02 -0.5862606 2009-04-10 12:00:00
3 2009-04-10 12:00:03 0.1997259 2009-04-10 12:00:00
4 2009-04-10 12:01:01 -1.7562755 2009-04-10 12:01:00
5 2009-04-10 12:01:02 0.9603029 2009-04-10 12:01:00
6 2009-04-10 12:02:02 0.6520215 2009-04-10 12:02:00
7 2009-04-10 12:02:15 -0.5020440 2009-04-10 12:02:00
8 2009-04-10 12:03:20 -0.9209041 2009-04-10 12:03:00
Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com
-------------------------------------------------------------------
[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.U
TF-8;LC_MONETARY=C;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAMEC;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATI
ON=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base