as.POSIXct gives an unexpected timezone

前端 未结 4 580
囚心锁ツ
囚心锁ツ 2020-12-16 16:17

I\'m trying to convert a yearmon date (from the zoo package) to a POSIXct in the UTC timezone. This is what I tried to do:

> as.POSIXct(as.yearmon(\"2010         


        
4条回答
  •  难免孤独
    2020-12-16 16:45

    This seems to be an oddity with the date/time "POSIXct" class methods. Try formatting the "Date" or "yearmon" variable first so that as.POSIXct.character rather than as.POSIXct.{Date, yearmon} is dispatched:

    Date

    > d <- as.Date("2010-01-01")
    > as.POSIXct(format(d), tz = "UTC")
    [1] "2010-01-01 UTC"
    

    yearmon

    > library(zoo)
    > y <- as.yearmon("2010-01")
    > as.POSIXct(format(y, format = "%Y-%m-01"), tz = "UTC")
    [1] "2010-01-01 UTC"
    > # or
    > as.POSIXct(format(as.Date(y)), tz = "UTC")
    [1] "2010-01-01 UTC"
    

提交回复
热议问题