[R] foreloop? aggregating time series data into groups

jim holtman jholtman at gmail.com
Mon Nov 1 22:15:44 CET 2010


you can use na.locf in the zoo package:

> require(zoo)
> x<-c(0,2,0,1,0,0,0,0,1,0,1,0,0,0,2,1,0,0,0,2,0,0,0,1)
> # replace zeros with NA
> x[x == 0] <- NA
> x
 [1] NA  2 NA  1 NA NA NA NA  1 NA  1 NA NA NA  2  1 NA NA NA  2 NA NA NA  1
> na.locf(x, fromLast = TRUE)
 [1] 2 2 1 1 1 1 1 1 1 1 1 2 2 2 2 1 2 2 2 2 1 1 1 1
>


On Mon, Nov 1, 2010 at 3:34 PM, blurg <ian.jhsph at gmail.com> wrote:
>
> I have a data set similar to the set below where 1 and 2 indicate test
> results and 0 indicates time points in between where there are no test
> results.  I would like to allocate the time points leading up to a test
> result with the value of the test result.
>
> What I have:     What I want:
> 1                     1
> 0                     1
> 0                     1
> 0                     1
> 1                     1
> 0                     2
> 0                     2
> 2                     2
> 0                     1
> 0                     1
> 1                     1
> 0                     2
> 2                     2
>
> I have attempted methods creating a data.frame of the the breaks/changes in
> of values to from 0 to 1 or to 2.
> x<-c(0,2,0,1,0,0,0,0,1,0,1,0,0,0,2,1,0,0,0,2,0,0,0,1)
> x1 <- which(diff(x) == 1)
> x2 <- which(diff(x) == 2)
>
> What ever the solution, I can't be entered by hand due to the size of the
> dataset (>10 million and change). Any ideas?  This is my first time posting
> to this forum and I am relatively new to R, so please don't flame me to
> hard.  Desperate times call for desperate measures.  Thanks.
> --
> View this message in context: http://r.789695.n4.nabble.com/foreloop-aggregating-time-series-data-into-groups-tp3022667p3022667.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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