0

基本的に、連結された POSIXct 日付/時刻オブジェクトによってインデックス付けされた動物園オブジェクトがあります。すべて問題ないように見えますが、電話をかけると:

zoo$Close

返された列には、インデックスと価格の複製が表示されます。

データフレームを介して動物園オブジェクトを読み込んで作成している回り道と関係があるに違いありませんが、日付と時刻の列を同時に貼り付けながら、より合理化された方法でそれを行うには完全に途方に暮れていますインデックスを作成します。

私のファイルを読み取るためのいくつかのコード:

getData = function(filename){

    data = read.table(filename, col.names = c("Date", "Time", "High", "Low", "Open", "Close", "Volume", "Bid", "Ask", "BA Spread"))
    data1 = data
    data[["Date"]] = data[["Time"]] = NULL

    zoo = zoo(data)
    index(zoo) = as.POSIXct(paste(data1[["Date"]], data1[["Time"]]))

    return(zoo)
}

データは 1 分間隔で、次のように空白で区切られたファイルに格納されます: (ここで行が少しはみ出します)

2012-06-01 06:00 134.32812500 134.31250000 134.32812500 134.32812500 710 134.32812500  134.34375000 0.01596653

2012-06-01 06:01 134.34375000 134.32812500 134.32812500 134.34375000 127 134.34375000 134.35937500 0.01566084

Zoo$Close を呼び出した後、次のような内容が表示されます。

2012-06-01 06:00 134.32812500

2012-06-01 06:00 134.32812500

2012-06-01 06:01 134.34375000

2012-06-01 06:01 134.34375000

私はここで私のロープの終わりにいます.Rにはまだ比較的慣れていませんが、どんなガイダンスも非常に高く評価されます.

編集: dput(head(data)) からの出力:

dput(ヘッド(データ))

structure(list(Date = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("2012-06-01", 
"2012-06-04", "2012-06-05", "2012-06-06", "2012-06-07", "2012-06-08", 
"2012-06-11", "2012-06-12", "2012-06-13", "2012-06-14", "2012-06-15", 
"2012-06-18", "2012-06-19", "2012-06-20", "2012-06-21", "2012-06-22", 
"2012-06-25", "2012-06-26", "2012-06-27", "2012-06-28", "2012-06-29"
), class = "factor"), Time = structure(1:6, .Label = c("06:00", 
"06:01", "06:02", "06:03", "06:04", "06:05", "06:06", "06:07", 
"06:08", "06:09", "06:10", "06:11", "06:12", "06:13", "06:14", 
"06:15", "06:16", "06:17", "06:18", "06:19", "06:20", "06:21", 
"06:22", "06:23", "06:24", "06:25", "06:26", "06:27", "06:28", 
"06:29", "06:30", "06:31", "06:32", "06:33", "06:34", "06:35", 
"06:36", "06:37", "06:38", "06:39", "06:40", "06:41", "06:42", 
"06:43", "06:44", "06:45", "06:46", "06:47", "06:48", "06:49", 
"06:50", "06:51", "06:52", "06:53", "06:54", "06:55", "06:56", 
"06:57", "06:58", "06:59", "07:00", "07:01", "07:02", "07:03", 
"07:04", "07:05", "07:06", "07:07", "07:08", "07:09", "07:10", 
"07:11", "07:12", "07:13", "07:14", "07:15", "07:16", "07:17", 
"07:18", "07:19", "07:20", "07:21", "07:22", "07:23", "07:24", 
"07:25", "07:26", "07:27", "07:28", "07:29", "07:30", "07:31", 
"07:32", "07:33", "07:34", "07:35", "07:36", "07:37", "07:38", 
"07:39", "07:40", "07:41", "07:42", "07:43", "07:44", "07:45", 
"07:46", "07:47", "07:48", "07:49", "07:50", "07:51", "07:52", 
"07:53", "07:54", "07:55", "07:56", "07:57", "07:58", "07:59", 
"08:00", "08:01", "08:02", "08:03", "08:04", "08:05", "08:06", 
"08:07", "08:08", "08:09", "08:10", "08:11", "08:12", "08:13", 
"08:14", "08:15", "08:16", "08:17", "08:18", "08:19", "08:20", 
"08:21", "08:22", "08:23", "08:24", "08:25", "08:26", "08:27", 
"08:28", "08:29", "08:30", "08:31", "08:32", "08:33", "08:34", 
"08:35", "08:36", "08:37", "08:38", "08:39", "08:40", "08:41", 
"08:42", "08:43", "08:44", "08:45", "08:46", "08:47", "08:48", 
"08:49", "08:50", "08:51", "08:52", "08:53", "08:54", "08:55", 
"08:56", "08:57", "08:58", "08:59", "09:00", "09:01", "09:02", 
"09:03", "09:04", "09:05", "09:06", "09:07", "09:08", "09:09", 
"09:10", "09:11", "09:12", "09:13", "09:14", "09:15", "09:16", 
"09:17", "09:18", "09:19", "09:20", "09:21", "09:22", "09:23", 
"09:24", "09:25", "09:26", "09:27", "09:28", "09:29", "09:30", 
"09:31", "09:32", "09:33", "09:34", "09:35", "09:36", "09:37", 
"09:38", "09:39", "09:40", "09:41", "09:42", "09:43", "09:44", 
"09:45", "09:46", "09:47", "09:48", "09:49", "09:50", "09:51", 
"09:52", "09:53", "09:54", "09:55", "09:56", "09:57", "09:58", 
"09:59", "10:00", "10:01", "10:02", "10:03", "10:04", "10:05", 
"10:06", "10:07", "10:08", "10:09", "10:10", "10:11", "10:12", 
"10:13", "10:14", "10:15", "10:16", "10:17", "10:18", "10:19", 
"10:20", "10:21", "10:22", "10:23", "10:24", "10:25", "10:26", 
"10:27", "10:28", "10:29", "10:30", "10:31", "10:32", "10:33", 
"10:34", "10:35", "10:36", "10:37", "10:38", "10:39", "10:40", 
"10:41", "10:42", "10:43", "10:44", "10:45", "10:46", "10:47", 
"10:48", "10:49", "10:50", "10:51", "10:52", "10:53", "10:54", 
"10:55", "10:56", "10:57", "10:58", "10:59", "11:00", "11:01", 
"11:02", "11:03", "11:04", "11:05", "11:06", "11:07", "11:08", 
"11:09", "11:10", "11:11", "11:12", "11:13", "11:14", "11:15", 
"11:16", "11:17", "11:18", "11:19", "11:20", "11:21", "11:22", 
"11:23", "11:24", "11:25", "11:26", "11:27", "11:28", "11:29", 
"11:30", "11:31", "11:32", "11:33", "11:34", "11:35", "11:36", 
"11:37", "11:38", "11:39", "11:40", "11:41", "11:42", "11:43", 
"11:44", "11:45", "11:46", "11:47", "11:48", "11:49", "11:50", 
"11:51", "11:52", "11:53", "11:54", "11:55", "11:56", "11:57", 
"11:58", "11:59", "12:00", "12:01", "12:02", "12:03", "12:04", 
"12:05", "12:06", "12:07", "12:08", "12:09", "12:10", "12:11", 
"12:12", "12:13", "12:14", "12:15", "12:16", "12:17", "12:18", 
"12:19", "12:20", "12:21", "12:22", "12:23", "12:24", "12:25", 
"12:26", "12:27", "12:28", "12:29", "12:30", "12:31", "12:32", 
"12:33", "12:34", "12:35", "12:36", "12:37", "12:38", "12:39", 
"12:40", "12:41", "12:42", "12:43", "12:44", "12:45", "12:46", 
"12:47", "12:48", "12:49", "12:50", "12:51", "12:52", "12:53", 
"12:54", "12:55", "12:56", "12:57", "12:58", "12:59", "13:00", 
"13:01", "13:02", "13:03", "13:04", "13:05", "13:06", "13:07", 
"13:08", "13:09", "13:10", "13:11", "13:12", "13:13", "13:14", 
"13:15", "13:16", "13:17", "13:18", "13:19", "13:20", "13:21", 
"13:22", "13:23", "13:24", "13:25", "13:26", "13:27", "13:28", 
"13:29", "13:30", "13:31", "13:32", "13:33", "13:34", "13:35", 
"13:36", "13:37", "13:38", "13:39", "13:40", "13:41", "13:42", 
"13:43", "13:44", "13:45", "13:46", "13:47", "13:48", "13:49", 
"13:50", "13:51", "13:52", "13:53", "13:54", "13:55", "13:56", 
"13:57", "13:58", "13:59", "14:00", "14:01", "14:02", "14:03", 
"14:04", "14:05", "14:06", "14:07", "14:08", "14:09", "14:10", 
"14:11", "14:12", "14:13", "14:14", "14:15", "14:16", "14:17", 
"14:18", "14:19", "14:20", "14:21", "14:22", "14:23", "14:24", 
"14:25", "14:26", "14:27", "14:28", "14:29", "14:30", "14:31", 
"14:32", "14:33", "14:34", "14:35", "14:36", "14:37", "14:38", 
"14:39", "14:40", "14:41", "14:42", "14:43", "14:44", "14:45", 
"14:46", "14:47", "14:48", "14:49", "14:50", "14:51", "14:52", 
"14:53", "14:54", "14:55", "14:56", "14:57", "14:58", "14:59", 
"15:00"), class = "factor"), High = c(134.328125, 134.34375, 
134.359375, 134.34375, 134.3125, 134.3125), Low = c(134.3125, 
134.328125, 134.328125, 134.3125, 134.28125, 134.28125), Open = c(134.328125, 
134.328125, 134.359375, 134.328125, 134.3125, 134.296875), Close = c(134.328125, 
134.34375, 134.328125, 134.3125, 134.28125, 134.296875), Volume = c(710L, 
127L, 2476L, 280L, 1064L, 1063L), Bid = c(134.328125, 134.34375, 
134.328125, 134.3125, 134.28125, 134.28125), Ask = c(134.34375, 
134.359375, 134.34375, 134.328125, 134.296875, 134.296875), BA.Spread = c(0.01596653, 
0.01566084, 0.01569884, 0.01568592, 0.015625, 0.01602079)), .Names = c("Date", 
"Time", "High", "Low", "Open", "Close", "Volume", "Bid", "Ask", 
"BA.Spread"), row.names = c(NA, 6L), class = "data.frame")

編集 2: sessionInfo() を実行しました

R version 2.14.2 (2012-02-29)
Platform: x86_64-suse-linux-gnu (64-bit)

locale:
[1] C

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
 [1] PerformanceAnalytics_1.0.4.4 quantmod_0.3-17             
 [3] Defaults_1.1-1               forecast_3.22               
 [5] RcppArmadillo_0.3.2.3        Rcpp_0.9.10                 
 [7] fracdiff_1.4-1               tseries_0.10-28             
 [9] quadprog_1.5-4               TTR_0.21-1                  
[11] xts_0.8-6                    zoo_1.7-7                   

loaded via a namespace (and not attached):
[1] grid_2.14.2    lattice_0.20-0

最終編集: 提案どおりにロードされた Zoo パッケージのみを使用して新しい R セッションを開始しましたが、同じ結果が返されました。ただし、これにより、うまくいく結果が得られました。私はこの問題について 24 時間ずっと考えてきました。Zoo オブジェクトをデータ フレームとして一時的にキャストすることで、zoo オブジェクト列の呼び出しを完全にバイパスしました。

getCol = function(zooObject, colname){
    frame = as.data.frame(zooObject)
    col = frame[[colname]]
    colzoo = zoo(col)
    index(colzoo) = index(zooObject)

    return(colzoo)
}

神に感謝します。ガイダンスを提供してくれた GSee に感謝します。

4

1 に答える 1

0

編集:出力をありがとうdputstringsAsFactors=TRUEのデフォルトであるを使用していますread.table。通話に追加stringsAsFactors=FALSEするだけで、準備完了です。read.table

data <- read.table(text="2012-06-01 06:00 134.32812500 134.31250000 134.32812500 134.32812500 710 134.32812500  134.34375000 0.01596653
2012-06-01 06:01 134.34375000 134.32812500 134.32812500 134.34375000 127 134.34375000 134.35937500 0.01566084",
col.names = c("Date", "Time", "High", "Low", "Open", "Close", "Volume", "Bid", "Ask", "BA Spread"), 
stringsAsFactors=FALSE)
data
#        Date  Time     High      Low     Open    Close Volume      Bid      Ask      BA.Spread
#1 2012-06-01 06:00 134.3281 134.3125 134.3281 134.3281    710 134.3281 134.3438 0.01596653
#2 2012-06-01 06:01 134.3438 134.3281 134.3281 134.3438    127 134.3438 134.3594 0.01566084
data1 = data
data[["Date"]] = data[["Time"]] = NULL
zoo = zoo(data)
index(zoo) = as.POSIXct(paste(data1[["Date"]], data1[["Time"]]))
zoo
#                        High      Low     Open    Close Volume      Bid      Ask  BA.Spread
#2012-06-01 06:00:00 134.3281 134.3125 134.3281 134.3281    710 134.3281 134.3438 0.01596653
#2012-06-01 06:01:00 134.3438 134.3281 134.3281 134.3438    127 134.3438 134.3594 0.01566084
zoo$Close
#2012-06-01 06:00:00 2012-06-01 06:01:00 
#           134.3281            134.3438 
于 2012-07-12T14:21:25.910 に答える