[R] Time Series filter help?
Rui Barradas
ruipbarradas at sapo.pt
Tue Sep 4 00:32:57 CEST 2012
Hello,
Try the following, where 'dat' is your data.frame.
dat <- read.table(text="
2012-05-07 0
2012-05-08 0
2012-05-09 0
[...etc...]
2012-08-27 1
2012-08-28 1
2012-08-29 1
", stringsAsFactors = FALSE)
r <- rle(dat[[2]])
csum <- cumsum(r$lengths > 3)
spl <- sapply(split(r$values, csum), `[`, 1)
dat$New <- rep(spl, tapply(r$lengths, csum, sum))
Hope this helps,
Rui Barradas
Em 03-09-2012 19:46, Douglas Karabasz escreveu:
> I have a time series data with 1's and 0's. When the last 3 observations
> are 1 I want to generate a 1 until I have three 0's in a row and then I want
> it to produce all zeros again. For example if I have 0101010101 for the
> first part of the time series this would produce all zeros. Then if the
> data has 111 my function will return 1's until I have three 0's in a row and
> it would keep producing zeros until I have three 1's in a row again.
> I have tried many ways to do this but I have not found a way that seems
> simple. I know I just making this more complex then needed so any help
> would be great.
> 2012-05-07 0
> 2012-05-08 0
> 2012-05-09 0
> 2012-05-10 0
> 2012-05-11 0
> 2012-05-14 0
> 2012-05-15 0
> 2012-05-16 0
> 2012-05-17 0
> 2012-05-18 0
> 2012-05-21 0
> 2012-05-22 0
> 2012-05-23 0
> 2012-05-24 0
> 2012-05-25 0
> 2012-05-29 0
> 2012-05-30 0
> 2012-05-31 0
> 2012-06-01 0
> 2012-06-04 0
> 2012-06-05 0
> 2012-06-06 0
> 2012-06-07 1
> 2012-06-08 0
> 2012-06-11 0
> 2012-06-12 0
> 2012-06-13 0
> 2012-06-14 1
> 2012-06-15 1
> 2012-06-18 1
> 2012-06-19 1
> 2012-06-20 1
> 2012-06-21 1
> 2012-06-22 0
> 2012-06-25 0
> 2012-06-26 0
> 2012-06-27 0
> 2012-06-28 0
> 2012-06-29 0
> 2012-07-02 1
> 2012-07-03 0
> 2012-07-05 1
> 2012-07-06 1
> 2012-07-09 0
> 2012-07-10 0
> 2012-07-11 0
> 2012-07-12 0
> 2012-07-13 0
> 2012-07-16 1
> 2012-07-17 1
> 2012-07-18 0
> 2012-07-19 0
> 2012-07-20 0
> 2012-07-23 0
> 2012-07-24 0
> 2012-07-25 0
> 2012-07-26 1
> 2012-07-27 1
> 2012-07-30 1
> 2012-07-31 1
> 2012-08-01 1
> 2012-08-02 1
> 2012-08-03 0
> 2012-08-06 1
> 2012-08-07 1
> 2012-08-08 1
> 2012-08-09 1
> 2012-08-10 1
> 2012-08-13 1
> 2012-08-14 1
> 2012-08-15 1
> 2012-08-16 1
> 2012-08-17 1
> 2012-08-20 1
> 2012-08-21 1
> 2012-08-22 1
> 2012-08-23 1
> 2012-08-24 1
> 2012-08-27 1
> 2012-08-28 1
> 2012-08-29 1
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
More information about the R-help
mailing list