Hi Gabor, I appreciate your suggestion. I'm wondering if there's any faster implementation that one could achieve. The dataset I have is about 400K rows and I have many of them. Just wondering if you have any thoughts. Thanks.
Sakda On Tue, Sep 8, 2009 at 6:57 AM, R_help Help<rhelp...@gmail.com> wrote: > ---------- Forwarded message ---------- > From: Gabor Grothendieck <ggrothendi...@gmail.com> > Date: Sun, Aug 30, 2009 at 11:08 PM > Subject: Re: [R-SIG-Finance] Aggregating irregular time series > To: R_help Help <rhelp...@gmail.com> > Cc: r-sig-fina...@stat.math.ethz.ch, r-help@r-project.org > > > 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<rhelp...@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-fina...@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. >> > ______________________________________________ R-help@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.