try this:
> x <- scan(textConnection("30/01/2007 0
+ 31/01/2007 -1
+ 01/02/2007 -1
+ 02/02/2007 -1
+ 03/02/2007 1
+ 04/02/2007 1
+ 05/02/2007 1
+ 06/02/2007 1
+ 07/02/2007 1
+ 08/02/2007 1
+ 09/02/2007 0
+ 10/02/2007 0
+ 11/02/2007 0
+ 12/02/2007 1
+ 13/02/2007 1
+ 14/02/2007 1
+ 15/02/2007 0
+ 16/02/2007 0
+ "), what=list(date="", value=0))
Read 18 records> x$date <- as.Date(x$date, "%d/%m/%Y")
> # determine the breaks
> x.breaks <- c(TRUE, diff(x$value) != 0)
> # determine the value at the break; assume that it is the minimum
> x.bdate <- x$date[x.breaks]
> data.frame(date=x.bdate[cumsum(x.breaks)], value=x$value)
date value
1 2007-01-30 0
2 2007-01-31 -1
3 2007-01-31 -1
4 2007-01-31 -1
5 2007-02-03 1
6 2007-02-03 1
7 2007-02-03 1
8 2007-02-03 1
9 2007-02-03 1
10 2007-02-03 1
11 2007-02-09 0
12 2007-02-09 0
13 2007-02-09 0
14 2007-02-12 1
15 2007-02-12 1
16 2007-02-12 1
17 2007-02-15 0
18 2007-02-15 0>
>
>
On 2/16/07, Murali Menon <feanor0@hotmail.com>
wrote:>
> Folks,
>
> I have a dataframe comprising a column of dates and a column of signals
> (-1,
> 0, 1) that looks something like this:
>
> 30/01/2007 0
> 31/01/2007 -1
> 01/02/2007 -1
> 02/02/2007 -1
> 03/02/2007 1
> 04/02/2007 1
> 05/02/2007 1
> 06/02/2007 1
> 07/02/2007 1
> 08/02/2007 1
> 09/02/2007 0
> 10/02/2007 0
> 11/02/2007 0
> 12/02/2007 1
> 13/02/2007 1
> 14/02/2007 1
> 15/02/2007 0
> 16/02/2007 0
>
> What I need to do is for each signal *in reverse chronological order* to
> find the date that it first appeared. So, for the zero on 16/02/2007 and
> 15/02/2007, the 'inception' date would be 15/02/2007, because the
day
> before, the signal was 1. Likewise, the 'inception' date for the
signal 1
> on
> 08/02/2007 and the five days prior, would be 03/02/2007. I need to create
> a
> structure of inception dates that would finally look as follows:
>
> -1 31/01/2007
> -1 31/01/2007
> -1 31/01/2007
> 1 03/02/2007
> 1 03/02/2007
> 1 03/02/2007
> 1 03/02/2007
> 1 03/02/2007
> 1 03/02/2007
> 0 09/02/2007
> 0 09/02/2007
> 0 09/02/2007
> 1 12/02/2007
> 1 12/02/2007
> 1 12/02/2007
> 0 15/02/2007
> 0 15/02/2007
>
> Is there a clever way of doing this? My sadly C-oriented upbringing can
> only
> think in terms of for-loops.
>
> Thanks!
>
> Murali
>
> _________________________________________________________________
> The average US Credit Score is 675. The cost to see yours: $0 by Experian.
>
> ______________________________________________
> R-help@stat.math.ethz.ch 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 you are trying to solve?
[[alternative HTML version deleted]]