Jeff Newmiller
2019-Sep-09 17:18 UTC
[R] Choosing specific Date Range from non-sequential Date
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 at 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 at 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 at 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 at 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.
Ogbos Okike
2019-Sep-09 19:14 UTC
[R] Choosing specific Date Range from non-sequential Date
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 at 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 at 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 at 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 at 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 at 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.
Jeff Newmiller
2019-Sep-09 19:22 UTC
[R] Choosing specific Date Range from non-sequential Date
Works for me. Apparently you are not showing us everything you are doing. As Bert recommended, you need to use the dput function rather than dumping numbers into the email if we are to know how your data are stored in memory which will affect what results you get. I highly recommend using the reprex package to verify that your example is reproducible. On September 9, 2019 12:14:50 PM PDT, Ogbos Okike <giftedlife2014 at gmail.com> wrote:>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 at 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 at 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 at 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 at 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 at 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.-- Sent from my phone. Please excuse my brevity.
William Dunlap
2019-Sep-09 20:09 UTC
[R] Choosing specific Date Range from non-sequential Date
To get a quick answer to your question you should provide a smallexample that one can simply copy and paste into an R session. It also helps to show some details about how something does not work, more than " But it didn't work." E.g., d <- read.table(header=FALSE, 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") names(d) <- c("date", "measurement") subset(d, date > "1997-12-15" & date < "1998-01-16") which gave: [1] date measurement <0 rows> (or 0-length row.names) Warning messages: 1: In Ops.factor(date, "1997-12-15") : ?>? not meaningful for factors 2: In Ops.factor(date, "1998-01-16") : ?<? not meaningful for factors People seeing the call to read.table() and the warning messages from subset() could quickly and confidently recommend adding the colClasses=c("Date", "numeric") to the call to read.table. Without that information, both questioners and helpers become frustrated. Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Sep 9, 2019 at 12:15 PM Ogbos Okike <giftedlife2014 at gmail.com> wrote:> 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 at 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 at 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 at 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 at 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 at 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 at 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]]
Rui Barradas
2019-Sep-09 20:35 UTC
[R] Choosing specific Date Range from non-sequential Date
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 at 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 at 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 at 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 at 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 at 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 at 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. >