あなたの出力に近い価格を記載しているもののに、それは企業活動など 株式分割のために調整されているので、代わりに 調整価格の列を使用することをお勧めします、配当金など
私は、テスト業界を使用していました実際の値に置き換える必要があります。
あなたは
new.env
を使用することができます
とlapply
次のように:
library(quantmod)
tickerVec = c("XLF","VFH","XLI","VIS","RWO","IYR","VNQI","VGT","RYT","VPU","IDU")
#test industry vector, replace with actual sector names
industryVec = c("industrials","financials","materials","energy",
"materials","energy","financials","technology","industrials","technology","energy")
startDt = as.Date("2012-01-01")
#create new data environment for storing all price timeseries
data.env = new.env()
getSymbols(tickerVec,env=data.env,src = "yahoo",from=startDt)
#convert to list class for ease in manipulation
data.env.lst = as.list(data.env)
#create an anoynmous function to reshape timeseries into required shape
fn_modifyData = function(x) {
TS = data.env.lst[[x]]
#xts to data.frame
TS_DF = data.frame(date=as.Date(index(TS)),coredata(TS),stringsAsFactors=FALSE)
#retain only required columns
TS_DF = TS_DF[,c(1,5,6)]
TS_DF$ticker = tickerVec[x]
TS_DF$industry = industryVec[x]
colnames(TS_DF) = c("date","close","volume","ticker","industry")
row.names(TS_DF) = NULL
return(TS_DF)
}
出力:
#apply function to all timeseries using lapply
outList = lapply(1:length(data.env.lst),function(z) fn_modifyData(z))
head(outList[[1]])
# date close volume ticker industry
#1 2012-01-03 13.34 103362000 XLF industrials
#2 2012-01-04 13.30 69833900 XLF industrials
#3 2012-01-05 13.48 89935300 XLF industrials
#4 2012-01-06 13.40 83878600 XLF industrials
#5 2012-01-09 13.47 69189600 XLF industrials
#6 2012-01-10 13.71 86035100 XLF industrials
head(outList[[11]])
# date close volume ticker industry
#1 2012-01-03 50.55 6100 IDU energy
#2 2012-01-04 50.41 2700 IDU energy
#3 2012-01-05 50.83 1700 IDU energy
#4 2012-01-06 50.82 7700 IDU energy
#5 2012-01-09 51.25 1800 IDU energy
#6 2012-01-10 51.71 5500 IDU energy
#if you wish to combine all datasets
outDF = do.call(rbind,outList)
head(outDF)
# date close volume ticker industry
#1 2012-01-03 13.34 103362000 XLF industrials
#2 2012-01-04 13.30 69833900 XLF industrials
#3 2012-01-05 13.48 89935300 XLF industrials
#4 2012-01-06 13.40 83878600 XLF industrials
#5 2012-01-09 13.47 69189600 XLF industrials
#6 2012-01-10 13.71 86035100 XLF industrials
あなたが実際にここでは "lapply" しようとしていないようでした。具体的には機能しなかったのは何ですか?あなたはこの機能にどのような入力をしたいのですか?また、出力をどのようにしたいですか? – MrFlick