[R] Choosing specific Date Range from non-sequential Date

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Mon Sep 9 22:35:45 CEST 2019


Hello,

I cannot reproduce the error but I coerce to class "Date" first.


MOSCFD50[[1]] <- as.Date(MOSCFD50[[1]])
names(MOSCFD50) <- c("date", "value")

# Your subset
Year <- subset(MOSCFD50, date > "1998-01-01" & date < "2005-12-31")

# Another way, if there are NA's, use which(i)
i <- MOSCFD50$date > "1998-01-01" & MOSCFD50$date < "2005-12-31"
Year2 <- MOSCFD50[i, ]

identical(Year, Year2)
#[1] TRUE


Data:


MOSCFD50 <- read.table(text = "
1997-11-23 -2.91709629064653
1997-12-07 -0.960255426066815
1997-12-11 -1.98210752999868
1997-12-20 -1.10800598439855
1998-01-01 -1.00090115428118
1998-01-29 -1.03056081882709
1998-03-27 -0.873243859498216
1998-04-09 -2.06378384750109
1998-04-12 -2.06826431469008
1998-04-19 -2.49834620746286
1998-05-02 -6.4357083781542
1998-05-17 -2.25359807972754
1998-05-21 -2.55799006865995
1999-08-22 -2.25114162617707
1999-08-25 -1.47905397376409
1999-09-05 -0.641589808755325
1999-09-09 -0.648954682695949
1999-09-13 -0.726364489272492
1999-09-16 -1.28445236942011
")


Hope this helps,

Rui Barradas


Às 20:14 de 09/09/19, Ogbos Okike escreveu:
> Dear Bert and Jeff,
> Thank you for looking at this.
> I am surprised my message was not in plain text. It has been
> configured to send message to the list in plain text earlier before.
> 
> The data again please.
> 
> 1997-11-23 -2.91709629064653
> 1997-12-07 -0.960255426066815
> 1997-12-11 -1.98210752999868
> 1997-12-20 -1.10800598439855
> 1998-01-01 -1.00090115428118
> 1998-01-29 -1.03056081882709
> 1998-03-27 -0.873243859498216
> 1998-04-09 -2.06378384750109
> 1998-04-12 -2.06826431469008
> 1998-04-19 -2.49834620746286
> 1998-05-02 -6.4357083781542
> 1998-05-17 -2.25359807972754
> 1998-05-21 -2.55799006865995
> 1999-08-22 -2.25114162617707
> 1999-08-25 -1.47905397376409
> 1999-09-05 -0.641589808755325
> 1999-09-09 -0.648954682695949
> 1999-09-13 -0.726364489272492
> 1999-09-16 -1.28445236942011
> 
> The first column is  date and the second is another data of interest.
> 
> The date is long but note sequential. Runs randomly from 1953 to 2019.
> 
> I would like to select any range of date from year A to year B.
> 
> Thank you again for your help.
> Best
> Ogbos
> 
> 
> On Mon, Sep 9, 2019 at 6:18 PM Jeff Newmiller <jdnewmil using dcn.davis.ca.us> wrote:
>>
>> Or the column is not named "date", or it is a factor... and the question is not posted in plain text....
>>
>> On September 9, 2019 9:55:48 AM PDT, Bert Gunter <bgunter.4567 using gmail.com> wrote:
>>> I would guess that you first need to convert your textual dates to a
>>> date-time object via as.date, but as you failed to provide a
>>> reproducible
>>> example (e.g. via dput) I may be wrong. Maybe others may have greater
>>> insight.
>>>
>>> Bert Gunter
>>>
>>> "The trouble with having an open mind is that people keep coming along
>>> and
>>> sticking things into it."
>>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>>>
>>>
>>> On Mon, Sep 9, 2019 at 8:55 AM Ogbos Okike <giftedlife2014 using gmail.com>
>>> wrote:
>>>
>>>> Dear Contributors,
>>>> I have a data frame of the form:
>>>> 1997-11-23 -2.91709629064653
>>>> 1997-12-07 -0.960255426066815
>>>> 1997-12-11 -1.98210752999868
>>>> 1997-12-20 -1.10800598439855
>>>> 1998-01-01 -1.00090115428118
>>>> 1998-01-29 -1.03056081882709
>>>> 1998-03-27 -0.873243859498216
>>>> 1998-04-09 -2.06378384750109
>>>> 1998-04-12 -2.06826431469008
>>>> 1998-04-19 -2.49834620746286
>>>> 1998-05-02 -6.4357083781542
>>>> 1998-05-17 -2.25359807972754
>>>> 1998-05-21 -2.55799006865995
>>>> 1999-08-22 -2.25114162617707
>>>> 1999-08-25 -1.47905397376409
>>>> 1999-09-05 -0.641589808755325
>>>> 1999-09-09 -0.648954682695949
>>>> 1999-09-13 -0.726364489272492
>>>> 1999-09-16 -1.28445236942011
>>>>
>>>> The events happen randomly and so the date is non-sequential. It run
>>> form
>>>> 1953 to 2019.
>>>>
>>>> I would like to select all the events/dates between 1998 to 2005.
>>>>
>>>> One of the things I tried is:
>>>> Year <- subset(MOSCFD50, date > "1998-01-01" & date < "2005-12-31").
>>>>
>>>> But it didn't work.
>>>>
>>>> I would be thankful if you could please redirect me.
>>>>
>>>> Thank you very much.
>>>> Best regards
>>>> Ogbos
>>>>
>>>>          [[alternative HTML version deleted]]
>>>>
>>>> ______________________________________________
>>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>> 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.
>>>>
>>>
>>>        [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> 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.
>>
>> --
>> Sent from my phone. Please excuse my brevity.
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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