Odd POSIXct Function Behavior In R

后端 未结 2 1244
醉梦人生
醉梦人生 2021-01-19 14:50

I\'m working with the POSIXct data type in R. In my work, I incorporate a function that returns two POSIXct dates in a vector. However, I am discovering some unexpected beha

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-19 15:47

    The problem is that the function c removes the timezone attribute:

    attributes(myTime)
    #$class
    #[1] "POSIXct" "POSIXt" 
    #
    #$tzone
    #[1] "UTC"
    
    attributes(c(myTime))
    #$class
    #[1] "POSIXct" "POSIXt"
    

    To fix, you can e.g. use the setattr function from data.table, to modify the attribute in place:

    (setattr(c(myTime), 'tzone', attributes(myTime)$tzone))
    #[1] "2015-01-01 UTC"
    

提交回复
热议问题