2017-02-07 7 views
0

Rを使用して複数の企業の過去の株価をExcel文書にエクスポートしたいと思います。異なる列のすべての価格をエクスポートする方法に問題があります。複数の会社の株価をExcelにエクスポートするR

たとえば、Appleの調整後の価格をExcelシートに書き出すことはできますが、他の会社を次の列に追加する方法などはわかりません。コードは次のとおりです。

library(quantmod) 
library(xlsx) 
getSymbols("AAPL", from="2014-01-01", to ="2017-01-01") 
write.xlsx(AAPL[,6], file.choose(), row.names=TRUE) 

誰でも解決策がありますか?

+0

'write.xlsx'はdata.frameで動作するように設計されているので、おそらく1に変換するのが最も簡単です。あなたは日付を失うことがないようにrownamesを列にすることができます。 – alistaire

答えて

0

これは、あなたが望むことをします。

# assumes codes are known beforehand 
codes <- c("MSFT","SBUX","S","AAPL","ADT") 
urls <- paste0("https://www.google.com/finance/historical?q=",codes,"&output=csv") 
paths <- paste0(codes,"csv") 
missing <- !(paths %in% dir(".", full.name = TRUE)) 
missing 

# simple error handling in case file doesn't exists 
downloadFile <- function(url, path, ...) { 
# remove file if exists already 
if(file.exists(path)) file.remove(path) 
# download file 
tryCatch(
download.file(url, path, ...), error = function(c) { 
# remove file if error 
if(file.exists(path)) file.remove(path) 
# create error message 
c$message <- paste(substr(path, 1, 4),"failed") 
message(c$message) 
} 
) 
} 
# wrapper of mapply 
Map(downloadFile, urls[missing], paths[missing]) 

空き時間があるときにこれを確認してください。

https://gist.github.com/jaehyeon-kim/356cf62b61248193db25#file-downloadstockdata

関連する問題