Problem with my function using as.POSIXct

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with my function using as.POSIXct

Thomas Adams-2
Hello all:

I wrote a function:

my.bastimeToSynoptic <- function(x) {
    f<-unlist(strsplit(as.character(x), " "))
    hr<-unlist(strsplit(f[2], ":"))
    if(as.numeric(hr[1])<6) {
        synoptic<-"00"
    }
    else {
        synoptic<-as.integer(as.numeric(hr[1])/6)*6
    }
    tdate<-paste(c(f[1]," ",as.character(synoptic),":00:00"),collapse="")
    d<-as.POSIXct(tdate, tz="EST")
    return(d)
}

This works as expected:

> my.bastimeToSynoptic("2010-12-01 14:05:00")
[1] "2010-12-01 12:00:00 EST"

This does not:
> my.bastimeToSynoptic("2010-12-01 05:05:00")
[1] "2010-12-01 EST"

I expect to get:
"2010-12-01 00:00:00 EST"

I've tried explicitly forcing the format with d<-as.POSIXct(tdate,
tz="EST", format = "%Y-%m-%d %H:%M:%S") and I have checked online, finding
examples showing what I'm doing with as.POSIXct should work. I'm at an
impasse.

I'm running R version 3.4.0 (2017-04-21) on Ubuntu 16.04 LTS

Thank you,
Tom


--

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Problem with my function using as.POSIXct

Jeff Newmiller
You have succeeded, but you just don't know it. The POSIXct object is representing that instant of time internally... you are just complaining about how it is printing it. So convert it to character explicitly with the desired format when you want to print it.

as.character( my.bastimeToSynoptic("2010-12-01 05:05:00"), format="%Y-%m-%d %H:%M:%S" )
--
Sent from my phone. Please excuse my brevity.

On March 28, 2018 8:44:43 PM PDT, Thomas Adams <[hidden email]> wrote:

>Hello all:
>
>I wrote a function:
>
>my.bastimeToSynoptic <- function(x) {
>    f<-unlist(strsplit(as.character(x), " "))
>    hr<-unlist(strsplit(f[2], ":"))
>    if(as.numeric(hr[1])<6) {
>        synoptic<-"00"
>    }
>    else {
>        synoptic<-as.integer(as.numeric(hr[1])/6)*6
>    }
>  tdate<-paste(c(f[1]," ",as.character(synoptic),":00:00"),collapse="")
>    d<-as.POSIXct(tdate, tz="EST")
>    return(d)
>}
>
>This works as expected:
>
>> my.bastimeToSynoptic("2010-12-01 14:05:00")
>[1] "2010-12-01 12:00:00 EST"
>
>This does not:
>> my.bastimeToSynoptic("2010-12-01 05:05:00")
>[1] "2010-12-01 EST"
>
>I expect to get:
>"2010-12-01 00:00:00 EST"
>
>I've tried explicitly forcing the format with d<-as.POSIXct(tdate,
>tz="EST", format = "%Y-%m-%d %H:%M:%S") and I have checked online,
>finding
>examples showing what I'm doing with as.POSIXct should work. I'm at an
>impasse.
>
>I'm running R version 3.4.0 (2017-04-21) on Ubuntu 16.04 LTS
>
>Thank you,
>Tom
>
>
>--
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.