[R] Indicator function for merged times (was: Re: Rr: For and if confusion)

Gabor Grothendieck ggrothendieck at gmail.com
Thu Dec 18 15:44:20 CET 2008


My initial comment was wrong. I see that this is really
an answer rather than the question but it was confusing
because it started a new thread.  Please try to use the
same thread in answering the question.

On Thu, Dec 18, 2008 at 9:39 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> Please start a new thread for a new topic and use a
> meaningful subject for sake of the archives and
>  everyone trying to follow.
>
> Using your Int1, correcting your Int2 and using your
> Y (which goes to Jul not Aug as per the comment):
>
> library(zoo)
>> as.ts(with(merge.zoo(Int1, Int2, Y, fill = 0), pmax(Int1, Int2)))
>     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
> 1992                               0   0   0   0   0
> 1993   0   0   1   1   1   1   1   1   1   1   1   1
> 1994   1   1   1   1   1   1   1   1   1   1   1   1
> 1995   1   1   1   1   1   1   1   1   1   1   1   1
> 1996   1   1   1   1   1   1   1   1   1   1   1   1
> 1997   1   1   1   1   1   1   1   1   1   1   1   1
> 1998   1   1   1   1   1   1   1   1   1   1   1   1
> 1999   1   1   1   1   1   1   1   1   0   0   0   0
> 2000   0   0   0   0   0   0   0   0   0   0   0   0
> 2001   0   0   0   0   0   0   0   0   0   0   0   0
> 2002   0   0   0   0   0   0   0
>
> On Thu, Dec 18, 2008 at 7:43 AM, Matthieu Stigler
> <matthieu.stigler at gmail.com> wrote:
>>
>>
>>> I have two date objects
>>>
>>> X <- c("01-03-1993", "01-05-1997") #Mar 1993 and May 1997
>>>
>>> Y <- c("01-02-1995", "01-08-1999") #Feb 1995 and Aug 1999
>>>
>>> and a time series object
>>>
>>> A <- ts(rnorm(120), freq=12, start=c(1992,8)) #Aug 1992 to Aug 2002
>>>
>>> I want to create a binary (0-1) vector B that is of length 1:(A).
>>>
>>> B should have value 1 for the time periods *across* my character vectors,
>>> i.e. between Mar 93 ad Feb 95, and also between May-97 and Aug-99, but zero
>>> otherwise.
>>>
>>> Would anyone have any ideas on how to do this? I would paste the code I am
>>> trying, but it would confuse rather than clarify (R newbie!). Am getting
>>> really lost in 'for' and 'if' loops, so would really appreciate any help!
>>>
>>> Thanks!
>>>
>>> ShruthiRE
>>
>> #time series of 1 in the interval
>> Int1<-ts(1, start=c(1993, 3), end=c(1997, 5), freq=12)
>> Int2<-ts(1, start=c(1993, 3), end=c(1997, 5), freq=12)
>> #original time series
>> Y <- ts(rnorm(120), freq=12, start=c(1992,8)) #Aug 1992 to Aug 2002
>>
>> #need zoo and as.zoo because time(ts) I don't see how to use time(ts) nicely
>> library(zoo)
>>
>> #condition and values
>> ifelse(time(as.zoo(Y))%in% time(as.zoo(Int1)),1,0)
>>
>> ______________________________________________
>> 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.
>>
>
> ______________________________________________
> 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