在r中使用XTS作为变量的名称

时间:2018-11-18 00:01:45

标签: r xts

我通过getsymbols()下载了300多种股票,并且在矢量中有这些股票的名称,例如:

USA_STOCKS = c("AAL","AAPL","ADBE","ADI","ADP","ADSK","ALGN",
               "ALXN","AMAT","AMGN","AMZN","ASML","ATVI","AVGO",
               "BIDU","BIIB") # This is just an extract from 300
getSymbols(AAL) # this is just one of the 300 "getsymbols"

有了这个,我有了一个名为AAL的XTS对象和一个带有所有XTS Objet名称的向量USA_TOCKS。

我想这样做:

AAL = na.omit(AAL)

但是,我不想使用对象AAL,而是要使用向量内部的名称来引用对象。像这样:

USA_STOCKS[1] = na.omit(USA_STOCKS[1])

如果这样做,我将仅在向量内部更改“ AAL”的名称。但是我要引用的对象是AAL。

1 个答案:

答案 0 :(得分:1)

嗯,还是有点不清楚,但是我想你想做这样的事情:

library(quantmod)

USA_STOCKS = c("AAL","AAPL","ADBE")

# Put all requested quotes in big list
stocks_usa <- lapply(USA_STOCKS,
                     getSymbols,
                     from = "2018-10-01",
                     to = "2018-11-01",
                     auto.assign = F)

# set the names of the list
names(stocks_usa) <- USA_STOCKS

#reference AAL
head(stocks_usa$AAL)

           AAL.Open AAL.High AAL.Low AAL.Close AAL.Volume AAL.Adjusted
2018-10-01    41.41    41.75   39.60     39.61    7210700     39.50097
2018-10-02    39.60    39.60   38.40     38.50    7625000     38.39403
2018-10-03    38.70    39.26   38.42     38.80    6370300     38.69320
2018-10-04    38.80    39.01   37.48     37.92    5916500     37.81562
2018-10-05    37.93    38.13   36.21     36.44    9127000     36.33969
2018-10-08    36.44    36.85   35.60     35.90    7879300     35.80119

# more referencing
stocks_usa$AAL <- na.omit(stocks_usa$AAL)