时区,period.apply在xts中使用R

时间:2012-08-09 19:29:01

标签: r timezone apply xts posixct

我在带有GMT时区的xts对象中有一堆1分钟的数据。我打电话给

period.apply (obj, endpoints(obj, "hours"))

由于某种原因,我的新对象的时区变为EST / EDT,但是,当我str(obj)时,它仍然说时区是GMT。这是一个例子:

obj1 <- xts(1:200, order.by=index(ret_1_min_xts)[1:200])
head(obj1)
                    [,1]
1986-02-04 14:32:00    1
1986-02-04 14:33:00    2
1986-02-04 14:34:00    3
1986-02-04 14:35:00    4
1986-02-04 14:36:00    5
1986-02-04 14:37:00    6
Warning message:
   timezone of object (GMT) is different than current timezone (). 

现在做period.apply

obj2 <- period.apply(obj1, endpoints(obj1, "hours"), mean)
head(obj2)
                     [,1]
1986-02-04 09:59:00  12.5
1986-02-04 10:59:00  51.0
1986-02-04 11:59:00 103.5
1986-02-04 12:59:00 154.0
1986-02-04 13:26:00 189.5

str(obj1)
An ‘xts’ object from 1986-02-04 14:32:00 to 1986-02-04 18:26:00 containing:
   Data: int [1:200, 1] 1 2 3 4 5 6 7 8 9 10 ...
   Indexed by objects of class: [POSIXct,POSIXt] TZ: GMT
   xts Attributes:  
   List of 2
    $ tclass: chr [1:2] "POSIXct" "POSIXt"
    $ tzone : chr "GMT"

str(obj2)
An ‘xts’ object from 1986-02-04 09:59:00 to 1986-02-04 13:26:00 containing:
   Data: num [1:5, 1] 12.5 51 103.5 154 189.5
   Indexed by objects of class: [POSIXct,POSIXt] TZ: 
   xts Attributes:  
   List of 2
    $ tclass: chr [1:2] "POSIXct" "POSIXt"
    $ tzone : chr "GMT"

1 个答案:

答案 0 :(得分:3)

这是一个很大的线索

Warning message:
  timezone of object (GMT) is different than current timezone ()

它会显示在您系统的时区中。您可以像这样设置系统的时区

Sys.setenv(TZ="GMT")

然后,您的xts将使用GMT时区打印。