Ptit_Bleu
2007-Nov-20 11:53 UTC
[R] How to improve a function converting year, day of year, decimal hour into date ?
Hi,
I would like to "build" a date from a year, a day of the year and a
decimal
hour.
Ex : reconst_date(2007,324,12.50) gives "2007-11-20 12:30:0"
The following function is doing this job but I would like to know if there
is a function which directly converts the decimal hours into
hours:minutes:secondes ?
Thanks for your help,
Have a nice day,
Ptit Bleu.
--------------------
reconst_date<-function(annee, jour, heured) {
str_anneejour<-paste(annee,jour,sep=" ")
str_anneemoisjour<-strptime(str_anneejour, "%Y %j")
heure<-trunc(heured)
minute<-trunc((heured-heure)*60)
seconde<-((heured-heure)*60-minute)*60
seq_dateR<-paste(str_anneemoisjour,"
",heure,":",minute,":",round(seconde),sep="")
return(seq_dateR)
}
--
View this message in context:
http://www.nabble.com/How-to-improve-a-function-converting-year%2Cday-of-year%2Cdecimal-hour-into-date---tf4843032.html#a13855563
Sent from the R help mailing list archive at Nabble.com.
Gabor Grothendieck
2007-Nov-20 12:19 UTC
[R] How to improve a function converting year, day of year, decimal hour into date ?
library(chron)
chron(paste("1/1/", year, sep = "")) + julday + hour/24
That produces a chron, i.e. dates/times class datetime. If you want
it as a string use format(...above expression...) See the help desk
article in R News 4/1.
On Nov 20, 2007 6:53 AM, Ptit_Bleu <ptit_bleu at yahoo.fr>
wrote:>
> Hi,
>
> I would like to "build" a date from a year, a day of the year and
a decimal
> hour.
> Ex : reconst_date(2007,324,12.50) gives "2007-11-20 12:30:0"
>
> The following function is doing this job but I would like to know if there
> is a function which directly converts the decimal hours into
> hours:minutes:secondes ?
>
> Thanks for your help,
> Have a nice day,
> Ptit Bleu.
>
> --------------------
>
> reconst_date<-function(annee, jour, heured) {
> str_anneejour<-paste(annee,jour,sep=" ")
> str_anneemoisjour<-strptime(str_anneejour, "%Y %j")
> heure<-trunc(heured)
> minute<-trunc((heured-heure)*60)
> seconde<-((heured-heure)*60-minute)*60
> seq_dateR<-paste(str_anneemoisjour,"
>
",heure,":",minute,":",round(seconde),sep="")
> return(seq_dateR)
> }
> --
> View this message in context:
http://www.nabble.com/How-to-improve-a-function-converting-year%2Cday-of-year%2Cdecimal-hour-into-date---tf4843032.html#a13855563
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
Ptit_Bleu
2007-Nov-20 13:01 UTC
[R] How to improve a function converting year, day of year, decimal hour into date ?
Thank you Gabor for your fast answer (received as I was reading R News 4/1)
Ptit Bleu.
library(chron)
chron(paste("1/1/", year, sep = "")) + julday + hour/24
That produces a chron, i.e. dates/times class datetime. If you want
it as a string use format(...above expression...) See the help desk
article in R News 4/1.
--
View this message in context:
http://www.nabble.com/How-to-improve-a-function-converting-year%2Cday-of-year%2Cdecimal-hour-into-date---tf4843032.html#a13856584
Sent from the R help mailing list archive at Nabble.com.