Rで学ぶ金融データのインポートと管理
Joshua Ulrich
Quantitative Analyst & quantmod Co-Author and Maintainer
# 新しい環境を作成 data_env <- new.env()# getSymbols で環境にデータを読み込む getSymbols(c("SPY", "QQQ"), env = data_env, auto.assign = TRUE)
"SPY" "QQQ"
# SPY の先頭数行を確認
head(data_env$SPY, 3)
SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
2007-01-03 142.25 142.86 140.57 141.37 94807600 114.8094
2007-01-04 141.23 142.05 140.61 141.67 69620600 115.0530
2007-01-05 141.33 141.40 140.38 140.54 76645300 114.1353
do.call() で関数結果のリストを1つに結合what)は呼び出す関数args)は渡す引数のリスト# 各オブジェクトから調整後列を抽出 adjusted_list <- lapply(data_env, Ad)# 各リスト要素を1つのオブジェクトに結合 adjusted <- do.call(merge, adjusted_list)
head(adjusted)
QQQ.Adjusted SPY.Adjusted
2007-01-03 39.47694 114.8094
2007-01-04 40.22558 115.0530
2007-01-05 40.03385 114.1353
2007-01-08 40.06124 114.6632
2007-01-09 40.26210 114.5658
2007-01-10 40.73684 114.9475
Rで学ぶ金融データのインポートと管理