[R] [R-SIG-Finance] Aggregating irregular time series
Gabor Grothendieck
ggrothendieck at gmail.com
Mon Aug 31 05:08:11 CEST 2009
Try this for the first question:
neighborapply <- function(z, width, FUN) {
out <- z
ix <- seq_along(z)
jx <- findInterval(time(z) + width, time(z))
out[] <- mapply(function(i, j) FUN(c(0, z[seq(i+1, length = j-i)])), ix, jx)
out
}
# test - corrected :948 in last line
library(zoo)
library(chron)
Lines <- "Time x
10:00:00.021 20
10:00:00.224 20
10:00:01.002 19
10:00:02.948 20"
z <- read.zoo(textConnection(Lines), header = TRUE, FUN = times)
neighborapply(z, times("00:00:02"), sum)
# and here is an alternative neighborapply
# using loops. The non-loop solution does
# have the disadvantage that it does as
# readily extend to other situations which
# is why we add this second solution to
# the first question.
neighborapply <- function(z, width, FUN) {
out <- z
tt <- time(z)
for(i in seq_along(tt)) {
for(j in seq_along(tt)) {
if (tt[j] - tt[i] > width) break
}
if (j == length(tt) && tt[j] - tt[i] <= width) j <- j+1
out[i] <- FUN(c(0, z[seq(i+1, length = j-i-1)]))
}
out
}
The second question can be answered along the lines
of the first by modifying neighborapply in the loop solution
appropriately.
On Sun, Aug 30, 2009 at 9:38 PM, R_help Help<rhelpacc at gmail.com> wrote:
> Hi,
>
> I have a couple of aggregation operations that I don't know how to
> accomplish. Let me give an example. I have the following irregular
> time series
>
> time x
> 10:00:00.021 20
> 10:00:00.224 20
> 10:00:01.002 19
> 10:00:02:948 20
>
> 1) For each entry time, I'd like to get sum of x for the next 2
> seconds (excluding itself). Using the above example, the output should
> be
>
> time sumx
> 10:00:00.021 39
> 10:00:00.224 19
> 10:00:01.442 20
> 10:00:02:948 0
>
> 2) For each i-th of x in the series, what's the first passage time to
> x[i]-1. I.e. the output should be
>
> time firstPassgeTime
> 10:00:00.021 0.981
> 10:00:00.224 0.778
> 10:00:01.442 NA
> 10:00:02:948 NA
>
> Is there any shortcut function that allows me to do the above? Thank you.
>
> adschai
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>
More information about the R-help
mailing list