RFinanceYJで取得したデータをxtsに変換する
RFinanceYJで、xts型のデータを返す関数を追加するためのメモ
インストールと読み込み
> install.packages("xts") > library(xts) 要求されたパッケージ zoo をロード中です
サンプルデータ
> data(sample_matrix) > head(sample_matrix) Open High Low Close 2007-01-02 50.03978 50.11778 49.95041 50.11778 2007-01-03 50.23050 50.42188 50.23050 50.39767 2007-01-04 50.42096 50.42096 50.26414 50.332362007-01-05 50.37347 50.37347 50.22103 50.33459 2007-01-06 50.24433 50.24433 50.11121 50.18112 2007-01-07 50.13211 50.21561 49.99185 49.99185 > sample.xts <- as.xts(sample_matrix, descr='Sample Data !') > class(sample.xts) [1] "xts" "zoo" > str(sample.xts) An ‘xts’ object from 2007-01-02 to 2007-06-30 containing: Data: num [1:180, 1:4] 50 50.2 50.4 50.4 50.2 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:4] "Open" "High" "Low" "Close" Indexed by objects of class: [POSIXct,POSIXt] TZ: xts Attributes: List of 1 $ descr: chr "Sample Data !"
RFinanceYJで取ったデータをxtsに
> library(RFinanceYJ) 要求されたパッケージ XML をロード中です > yj <- quoteStockTsData("4689.t") > head(yj) date open height low close volume61 2010-12-21 32150 32500 32150 32350 44899 60 2010-12-22 32250 32450 31950 32100 63865 59 2010-12-24 31800 32050 31750 31900 66397 58 2010-12-27 32100 32200 31800 31850 36607 57 2010-12-28 31700 31850 31650 31800 25469 56 2010-12-29 31650 32000 31650 31950 22166 > yj.matrix <- as.matrix(yj[,-1])> colnames(yj.matrix) <- c("Ope", "High", "Low", "Close", "Volume") > rownames(yj.matrix) <- as.character(yj$date) > yj.xts <- as.xts(yj.matrix, descr='Yahoo! Japan Stock Data') > str(yj.xts) An ‘xts’ object from 2010-12-21 to 2011-03-23 containing: Data: num [1:61, 1:5] 32150 32250 31800 32100 31700 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:5] "Ope" "High" "Low" "Close" ... Indexed by objects of class: [POSIXct,POSIXt] TZ: xts Attributes: List of 1 $ descr: chr "Yahoo! Japan Stock Data" > head(yj.xts) Ope High Low Close Volume 2010-12-21 32150 32500 32150 32350 44899 2010-12-22 32250 32450 31950 32100 63865 2010-12-24 31800 32050 31750 31900 66397 2010-12-27 32100 32200 31800 31850 36607 2010-12-28 31700 31850 31650 31800 25469 2010-12-29 31650 32000 31650 31950 22166
出来た!