[R] Convert Long DOY time format to yyyymmdd hh format: Problem Fixed
Dear Gurus, I am so happy to thank you all for your great help!!! Jim's code first did it. Thanks again to everyone. Warmest regards Ogbos On Thu, Jan 23, 2020 at 10:42 AM Jim Lemon wrote: > > Hi Ogbos, > Try this: > > oodates<-read.table(text="1963 335 0 > 1963 335 1 > 1963 335 2 > 1963 335 3 > 1963 335 4 > 1963 335 5 > 1963 335 6 > 1963 335 7 > 1963 335 8 > 1963 335 9 > 1996 202 20 > 1996 202 21 > 1996 202 22 > 1996 202 23 > 1996 203 0 > 1996 203 1 > 1996 203 2 > 1996 203 3 > 2018 365 20 > 2018 365 21 > 2018 365 22 > 2018 365 23") > oodates$Pdate<-strptime(paste(oodates[,1],oodates[,2],oodates[,3]), > format="%Y %j %H") > > Jim > > On Thu, Jan 23, 2020 at 8:24 PM Ogbos Okike wrote: > > > > Dear Experts, > > I have a data spanning 56 years from 1963 to 2018. > > The datetime format is in DOY hour: > > 1963 335 0 > > 1963 335 1 > > 1963 335 2 > > 1963 335 3 > > 1963 335 4 > > 1963 335 5 > > 1963 335 6 > > 1963 335 7 > > 1963 335 8 > > 1963 335 9 > > 1996 202 20 > > 1996 202 21 > > 1996 202 22 > > 1996 202 23 > > 1996 203 0 > > 1996 203 1 > > 1996 203 2 > > 1996 203 3 > > 2018 365 20 > > 2018 365 21 > > 2018 365 22 > > 2018 365 23 > > When I used: > > as.Date(335,origin="1963-01-01"), for the first row, I got: > > [1] "1963-12-02" > > This is the format I want, though it is not yet complete. Time is missing. > > > > Again, I can't be doing this one after the other. I guess you have a > > better way of handling this. I have spent some time trying to get it > > right but I am really stuck. I would be most glad if you could spare > > your busy time to help me again. > > > > Thank you very much for your usual kind assistance. > > > > Best regards > > Ogbos > > > > __ > > R-help@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. __ R-help@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.
Re: [R] Convert Long DOY time format to yyyymmdd hh format
Jim's (and Petr's) solution wins -pd > On 23 Jan 2020, at 10:56 , peter dalgaard wrote: > > Here's an idea: > >> as.POSIXct(paste0("1963","-1-1"))+as.difftime(335,units="days") + >> as.difftime(3, units="hours") > [1] "1963-12-02 03:00:00 CET" > > However, 2 caveats > > (a) I think you need to subtract 1 from the DOY (1 should be Jan 1, right?) > (b) Beware Daylight Savings time: > >> as.POSIXct(paste0("2019","-1-1"))+as.difftime(160,units="days") + >> as.difftime(3, units="hours") > [1] "2019-06-10 04:00:00 CEST" > > This can be worked around as follows: > >> tt <- as.POSIXct(paste0("2019","-1-1"))+as.difftime(160,units="days") >> ttl <- as.POSIXlt(tt) >> ttl$hour=0 >> ttl + as.difftime(3, units="hours") > [1] "2019-06-10 03:00:00 CEST" > > ISOdate() and ISOdatetime() can also be used, again beware of time zones and > DST. Also, ISOdate gives 12:00 GMT, whereas the POSIX stuff gives 0:00. > > -pd > >> On 23 Jan 2020, at 10:24 , Ogbos Okike wrote: >> >> Dear Experts, >> I have a data spanning 56 years from 1963 to 2018. >> The datetime format is in DOY hour: >> 1963 335 0 >> 1963 335 1 >> 1963 335 2 >> 1963 335 3 >> 1963 335 4 >> 1963 335 5 >> 1963 335 6 >> 1963 335 7 >> 1963 335 8 >> 1963 335 9 >> 1996 202 20 >> 1996 202 21 >> 1996 202 22 >> 1996 202 23 >> 1996 203 0 >> 1996 203 1 >> 1996 203 2 >> 1996 203 3 >> 2018 365 20 >> 2018 365 21 >> 2018 365 22 >> 2018 365 23 >> When I used: >> as.Date(335,origin="1963-01-01"), for the first row, I got: >> [1] "1963-12-02" >> This is the format I want, though it is not yet complete. Time is missing. >> >> Again, I can't be doing this one after the other. I guess you have a >> better way of handling this. I have spent some time trying to get it >> right but I am really stuck. I would be most glad if you could spare >> your busy time to help me again. >> >> Thank you very much for your usual kind assistance. >> >> Best regards >> Ogbos >> >> __ >> R-help@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. > > -- > Peter Dalgaard, Professor, > Center for Statistics, Copenhagen Business School > Solbjerg Plads 3, 2000 Frederiksberg, Denmark > Phone: (+45)38153501 > Office: A 4.23 > Email: pd@cbs.dk Priv: pda...@gmail.com > > > > > > > > > -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd@cbs.dk Priv: pda...@gmail.com __ R-help@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.
Re: [R] Convert Long DOY time format to yyyymmdd hh format
Here's an idea: > as.POSIXct(paste0("1963","-1-1"))+as.difftime(335,units="days") + > as.difftime(3, units="hours") [1] "1963-12-02 03:00:00 CET" However, 2 caveats (a) I think you need to subtract 1 from the DOY (1 should be Jan 1, right?) (b) Beware Daylight Savings time: > as.POSIXct(paste0("2019","-1-1"))+as.difftime(160,units="days") + > as.difftime(3, units="hours") [1] "2019-06-10 04:00:00 CEST" This can be worked around as follows: > tt <- as.POSIXct(paste0("2019","-1-1"))+as.difftime(160,units="days") > ttl <- as.POSIXlt(tt) > ttl$hour=0 > ttl + as.difftime(3, units="hours") [1] "2019-06-10 03:00:00 CEST" ISOdate() and ISOdatetime() can also be used, again beware of time zones and DST. Also, ISOdate gives 12:00 GMT, whereas the POSIX stuff gives 0:00. -pd > On 23 Jan 2020, at 10:24 , Ogbos Okike wrote: > > Dear Experts, > I have a data spanning 56 years from 1963 to 2018. > The datetime format is in DOY hour: > 1963 335 0 > 1963 335 1 > 1963 335 2 > 1963 335 3 > 1963 335 4 > 1963 335 5 > 1963 335 6 > 1963 335 7 > 1963 335 8 > 1963 335 9 > 1996 202 20 > 1996 202 21 > 1996 202 22 > 1996 202 23 > 1996 203 0 > 1996 203 1 > 1996 203 2 > 1996 203 3 > 2018 365 20 > 2018 365 21 > 2018 365 22 > 2018 365 23 > When I used: > as.Date(335,origin="1963-01-01"), for the first row, I got: > [1] "1963-12-02" > This is the format I want, though it is not yet complete. Time is missing. > > Again, I can't be doing this one after the other. I guess you have a > better way of handling this. I have spent some time trying to get it > right but I am really stuck. I would be most glad if you could spare > your busy time to help me again. > > Thank you very much for your usual kind assistance. > > Best regards > Ogbos > > __ > R-help@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. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd@cbs.dk Priv: pda...@gmail.com __ R-help@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.
Re: [R] Convert Long DOY time format to yyyymmdd hh format
Hi as.Date converts to dates (without hours), which is clearly stated in the first line of documentation. If you want include hour you should use ?strptime Something like > strptime("1963 335 1", format="%Y %j %H") [1] "1963-12-01 01:00:00 CET" You definitely does not need to do it one by one, but exact way depends on how does the object look like in R. Cheers Petr > -Original Message- > From: R-help On Behalf Of Ogbos Okike > Sent: Thursday, January 23, 2020 10:24 AM > To: r-help > Subject: [R] Convert Long DOY time format to mmdd hh format > > Dear Experts, > I have a data spanning 56 years from 1963 to 2018. > The datetime format is in DOY hour: > 1963 335 0" > 1963 335 1 > 1963 335 2 > 1963 335 3 > 1963 335 4 > 1963 335 5 > 1963 335 6 > 1963 335 7 > 1963 335 8 > 1963 335 9 > 1996 202 20 > 1996 202 21 > 1996 202 22 > 1996 202 23 > 1996 203 0 > 1996 203 1 > 1996 203 2 > 1996 203 3 > 2018 365 20 > 2018 365 21 > 2018 365 22 > 2018 365 23 > When I used: > as.Date(335,origin="1963-01-01"), for the first row, I got: > [1] "1963-12-02" > This is the format I want, though it is not yet complete. Time is missing. > > Again, I can't be doing this one after the other. I guess you have a better way > of handling this. I have spent some time trying to get it right but I am really > stuck. I would be most glad if you could spare your busy time to help me > again. > > Thank you very much for your usual kind assistance. > > Best regards > Ogbos > > __ > R-help@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. __ R-help@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.
Re: [R] Convert Long DOY time format to yyyymmdd hh format
Hi Ogbos, Try this: oodates<-read.table(text="1963 335 0 1963 335 1 1963 335 2 1963 335 3 1963 335 4 1963 335 5 1963 335 6 1963 335 7 1963 335 8 1963 335 9 1996 202 20 1996 202 21 1996 202 22 1996 202 23 1996 203 0 1996 203 1 1996 203 2 1996 203 3 2018 365 20 2018 365 21 2018 365 22 2018 365 23") oodates$Pdate<-strptime(paste(oodates[,1],oodates[,2],oodates[,3]), format="%Y %j %H") Jim On Thu, Jan 23, 2020 at 8:24 PM Ogbos Okike wrote: > > Dear Experts, > I have a data spanning 56 years from 1963 to 2018. > The datetime format is in DOY hour: > 1963 335 0 > 1963 335 1 > 1963 335 2 > 1963 335 3 > 1963 335 4 > 1963 335 5 > 1963 335 6 > 1963 335 7 > 1963 335 8 > 1963 335 9 > 1996 202 20 > 1996 202 21 > 1996 202 22 > 1996 202 23 > 1996 203 0 > 1996 203 1 > 1996 203 2 > 1996 203 3 > 2018 365 20 > 2018 365 21 > 2018 365 22 > 2018 365 23 > When I used: > as.Date(335,origin="1963-01-01"), for the first row, I got: > [1] "1963-12-02" > This is the format I want, though it is not yet complete. Time is missing. > > Again, I can't be doing this one after the other. I guess you have a > better way of handling this. I have spent some time trying to get it > right but I am really stuck. I would be most glad if you could spare > your busy time to help me again. > > Thank you very much for your usual kind assistance. > > Best regards > Ogbos > > __ > R-help@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. __ R-help@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.
Re: [R] Convert Long DOY time format to yyyymmdd hh format
Quoting Ogbos Okike : Dear Experts, I have a data spanning 56 years from 1963 to 2018. The datetime format is in DOY hour: 1963 335 0 1963 335 1 1963 335 2 1963 335 3 1963 335 4 1963 335 5 1963 335 6 1963 335 7 1963 335 8 1963 335 9 1996 202 20 1996 202 21 1996 202 22 1996 202 23 1996 203 0 1996 203 1 1996 203 2 1996 203 3 2018 365 20 2018 365 21 2018 365 22 2018 365 23 When I used: as.Date(335,origin="1963-01-01"), for the first row, I got: [1] "1963-12-02" This is the format I want, though it is not yet complete. Time is missing. Again, I can't be doing this one after the other. I guess you have a better way of handling this. I have spent some time trying to get it right but I am really stuck. I would be most glad if you could spare your busy time to help me again. Thank you very much for your usual kind assistance. Best regards Ogbos Perhaps something like this: read.table(text=" 1963 335 0 1963 335 1 1963 335 2 1963 335 3 1963 335 4 1963 335 5 1963 335 6 1963 335 7 1963 335 8 1963 335 9 1996 202 20 1996 202 21 1996 202 22 1996 202 23 1996 203 0 1996 203 1 1996 203 2 1996 203 3 2018 365 20 2018 365 21 2018 365 22 2018 365 23 ", header = FALSE, sep = " ") -> data as.POSIXct(paste(as.Date(paste0(data[[1]], "-1-1")) + data[[2]] - 1, data[[3]]), format = "%Y-%m-%d %H") You might want to specify a different timezone, and also check for "off-by-one" error when it comes to day of year. -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net __ R-help@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.
[R] Convert Long DOY time format to yyyymmdd hh format
Dear Experts, I have a data spanning 56 years from 1963 to 2018. The datetime format is in DOY hour: 1963 335 0 1963 335 1 1963 335 2 1963 335 3 1963 335 4 1963 335 5 1963 335 6 1963 335 7 1963 335 8 1963 335 9 1996 202 20 1996 202 21 1996 202 22 1996 202 23 1996 203 0 1996 203 1 1996 203 2 1996 203 3 2018 365 20 2018 365 21 2018 365 22 2018 365 23 When I used: as.Date(335,origin="1963-01-01"), for the first row, I got: [1] "1963-12-02" This is the format I want, though it is not yet complete. Time is missing. Again, I can't be doing this one after the other. I guess you have a better way of handling this. I have spent some time trying to get it right but I am really stuck. I would be most glad if you could spare your busy time to help me again. Thank you very much for your usual kind assistance. Best regards Ogbos __ R-help@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.