[R] Problem with my function using as.POSIXct

Thomas Adams tea3rd at gmail.com
Thu Mar 29 05:44:43 CEST 2018


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]]



More information about the R-help mailing list