2016-12-16 6 views
0

私は自分のデータがUTCタイムゾーンにあるので、私はAmerica/New_Yorkにそれらをシフトすることができると教えてください。しかし、私がindexTZ()を使うと時間が変わります。xtsのタイムゾーンを変更する方法

16:00 UTC時間を12:00 NY時間にします。

test = read.zoo(paste0(datadir,"test_.csv"), 
        index = 1,FUN = as.POSIXct, header = T, sep = ",") 
    test = as.xts(test) 
    head(test) 


    > QQQ.Open QQQ.High QQQ.Low QQQ.Close QQQ.Volume 
    > 
    > 2016-09-10 16:38:00 4665.75 4665.75 4665.75 4665.75   1 
    > 2016-09-11 14:13:00 4665.75 4665.75 4665.75 4665.75   1 
    > 2016-09-11 22:01:00 4661.25 4667.25 4657.25 4666.75  932 
    > 2016-09-11 22:02:00 4666.75 4667.25 4663.25 4665.00  174 
    > 2016-09-11 22:03:00 4665.00 4667.00 4665.00 4666.50   66 


    indexTZ(test)<- "UTC" 
    head(test) 

        QQQ.Open QQQ.High QQQ.Low QQQ.Close QQQ.Volume 
2016-09-10 20:38:00 4665.75 4665.75 4665.75 4665.75   1 
2016-09-11 18:13:00 4665.75 4665.75 4665.75 4665.75   1 
2016-09-12 02:01:00 4661.25 4667.25 4657.25 4666.75  932 
2016-09-12 02:02:00 4666.75 4667.25 4663.25 4665.00  174 
2016-09-12 02:03:00 4665.00 4667.00 4665.00 4666.50   66 
Warning message: 
timezone of object (UTC) is different than current timezone(). 


> test_dt$hour1 = strftime(test_dt$index, format = "%H", tz = "America/New_York") 

> test_dt$hour2 = strftime(test_dt$index, format = "%H", tz = "UTC") 

> table(test_dt$hour1) 

14 16 22 
1 1 3 

> table(test_dt$hour2) 

02 18 20 
3 1 1 

答えて

2

シフトタイムゾーンは少しトリッキーです。まず最初に戻り、実際に保存された時刻が1970年1月1日からの秒数で表され、絶対時刻を表していることに気付きました。

R> format(as.POSIXct(0,origin="1970-01-01"), tz="UTC") 
[1] "1970-01-01" 
R> format(as.POSIXct(0,origin="1970-01-01"), tz="America/New_York") 
[1] "1969-12-31 19:00:00" 
R> format(as.POSIXct(0,origin="1970-01-01"), tz="Europe/Moscow") 
[1] "1970-01-01 03:00:00" 
R> 

今、あなたが保存されている時間は、おそらくlocaltimeのように解析した。つまり、私はNY、モスクワでの現地時間と同じ時刻(「エポック」)を表す方法をご覧ください。つまり、タイムゾーンとしてオフセットストアが含まれています。あなただけの前回に比べて移動することを変更することにより:だから私が正しくあなたの質問を理解して

R> as.POSIXct("2016-09-10 16:38:00") # CDT as I am in Chicago 
[1] "2016-09-10 16:38:00 CDT" 
R> format(as.POSIXct("2016-09-10 16:38:00"), tz="America/New_York") 
[1] "2016-09-10 17:38:00" 
R> format(as.POSIXct("2016-09-10 16:38:00"), tz="America/Los_Angeles") 
[1] "2016-09-10 14:38:00" 
R> 

、次の2つのことを行う必要があります:あなたが持っている地元の時間を「元に戻す」し、目的のタイムゾーンに移動します。

これはRcppCCTZパッケージのヘルパーも書いています。ここでtoTz()関数の一例は次のとおりです。

R> example(toTz) 

toTzR> toTz(Sys.time(), "America/New_York", "Europe/London") 
[1] "2016-12-17 01:04:14.184086 CST" 

toTzR> # this redoes the 'Armstrong on the moon in NYC and Sydney' example 
toTzR> # note that the default print method will print the return object in _your local time_ 
toTzR> toTz(ISOdatetime(1969,7,20,22,56,0,tz="UTC"), "America/New_York", "Australia/Sydney", verbose=TRUE) 
1969-07-20 22:56:00 -0400 
1969-07-21 12:56:00 +1000 
[1] "1969-07-20 21:56:00 CDT" 

toTzR> # whereas explicitly formating for Sydney time does the right thing 
toTzR> format(toTz(ISOdatetime(1969,7,20,22,56,0,tz="UTC"), 
toTz+    "America/New_York", "Australia/Sydney", verbose=TRUE), 
toTz+  tz="Australia/Sydney") 
1969-07-20 22:56:00 -0400 
1969-07-21 12:56:00 +1000 
[1] "1969-07-21 12:56:00" 

これはまた、あなたが希望の時間帯に印刷することを確実にする追加難しさを示して - 唯一の第二の例では、我々は、明示的にそれを使用するformat()に語ったように正しいシドニー時間を示してい。だから、戻ってあなたの例に取得する

R> x <- xts(1:2, Sys.time() + 0:1) 
R> x 
          [,1] 
2016-12-16 20:13:43.29767 1 
2016-12-16 20:13:44.29767 2 
R> tzone(x) <- "America/New_York" 
R> x 
          [,1] 
2016-12-16 21:13:43.29767 1 
2016-12-16 21:13:44.29767 2 
Warning message: 
timezone of object (America/New_York) is different than current timezone(). 
R> index(x) <- index(x) - 60*60 # dirty method, last resort 
R> x 
          [,1] 
2016-12-16 20:13:43.29767 1 
2016-12-16 20:13:44.29767 2 
Warning message: 
timezone of object (America/New_York) is different than current timezone(). 
R> tzone(x) 
       TZ 
"America/New_York" 
R> 

は、だから私は明示的に、シカゴから(私は時間単位で時間をシフトしているという事実を考慮して60分で私のローカルタイムを数値値を変更、 ニューヨークへ)。

2

おそらくタイムゾーンを "UTC"と指定していたはずですが、あなたのデータはおそらく "America/New_York"時間として読み込まれました。 (あなたがFUN=POSIXctのためのパススルーパラメータとして、タイムゾーンの引数を渡すことができるかもしれません。)

があなたの元の状況再現:

data <- " 2016-09-10 16:38:00 4665.75 4665.75 4665.75 4665.75   1 
2016-09-11 14:13:00 4665.75 4665.75 4665.75 4665.75   1 
2016-09-11 22:01:00 4661.25 4667.25 4657.25 4666.75  932 
2016-09-11 22:02:00 4666.75 4667.25 4663.25 4665.00  174 
2016-09-11 22:03:00 4665.00 4667.00 4665.00 4666.50   66" 

data = read.table(text = data, 
        col.names = c("date", "time", "Open" , "High", "Low", "Close", "Volume") 
       ) 
# assumes data is loaded in America/New_York time zone 
x_data <- xts(order.by = as.POSIXct(paste(data$date, data$time), tz = "America/New_York"), data[3:NCOL(data)]) 


x_data 
# Open High  Low Close Volume 
# 2016-09-10 16:38:00 4665.75 4665.75 4665.75 4665.75  1 
# 2016-09-11 14:13:00 4665.75 4665.75 4665.75 4665.75  1 
# 2016-09-11 22:01:00 4661.25 4667.25 4657.25 4666.75 932 
# 2016-09-11 22:02:00 4666.75 4667.25 4663.25 4665.00 174 
# 2016-09-11 22:03:00 4665.00 4667.00 4665.00 4666.50  66 

indexTZ(x_data) <- "UTC" 

# This reproduces your situation (problem): 
head(x_data) 
# Open High  Low Close Volume 
# 2016-09-10 20:38:00 4665.75 4665.75 4665.75 4665.75  1 
# 2016-09-11 18:13:00 4665.75 4665.75 4665.75 4665.75  1 
# 2016-09-12 02:01:00 4661.25 4667.25 4657.25 4666.75 932 
# 2016-09-12 02:02:00 4666.75 4667.25 4663.25 4665.00 174 
# 2016-09-12 02:03:00 4665.00 4667.00 4665.00 4666.50  66 


# This is what you probably wanted to do. Set the initial timezone to "UTC" when you loaded the data into R and created your `POSIXct` objects. 

x_data <- xts(order.by = as.POSIXct(paste(data$date, data$time), tz = "UTC"), data[3:NCOL(data)]) 
head(x_data) 
# Open High  Low Close Volume 
# 2016-09-10 16:38:00 4665.75 4665.75 4665.75 4665.75  1 
# 2016-09-11 14:13:00 4665.75 4665.75 4665.75 4665.75  1 
# 2016-09-11 22:01:00 4661.25 4667.25 4657.25 4666.75 932 
# 2016-09-11 22:02:00 4666.75 4667.25 4663.25 4665.00 174 
# 2016-09-11 22:03:00 4665.00 4667.00 4665.00 4666.50  66 
indexTZ(x_data) <- "America/New_York" 
# Now you get your desired outcome: 
head(x_data) 
# Open High  Low Close Volume 
# 2016-09-10 12:38:00 4665.75 4665.75 4665.75 4665.75  1 
# 2016-09-11 10:13:00 4665.75 4665.75 4665.75 4665.75  1 
# 2016-09-11 18:01:00 4661.25 4667.25 4657.25 4666.75 932 
# 2016-09-11 18:02:00 4666.75 4667.25 4663.25 4665.00 174 
# 2016-09-11 18:03:00 4665.00 4667.00 4665.00 4666.50  66 
0

アハを、はい、答えが正しいとXTSオブジェクトを作成することですタイムゾーン:

例:

S = as.xts(test, tz = "UTC")

関連する問題