从R列表中提取信息

时间:2013-06-11 15:03:42

标签: r

这是我之前的问题的后续,我试图从雅虎财经下载期权数据。它仍然无法正常工作。但我发现互联网上的另一个代码有效,但输出的格式我无法使用,因为我对R有点新。

代码给了我一个名为OptionPrices的变量。

以下是OptionPrices的输出,它使用命令fix(OptionPrices)打印为:


structure(list(call = structure(list(Strike = 26, Symbol = structure(1L, .Label = "VIXM131221C00026000", class = "factor"), 
    Last = 1.8, Chg = 0, Bid = 2.05, Ask = 2.65, Vol = 10L, Open.Int = 10L), .Names = c("Strike", 
"Symbol", "Last", "Chg", "Bid", "Ask", "Vol", "Open.Int"), row.names = c(NA, 
-1L), class = "data.frame"), put = structure(list(Strike = c(24, 
25, 26, 29), Symbol = structure(1:4, .Label = c("VIXM131221P00024000", 
"VIXM131221P00025000", "VIXM131221P00026000", "VIXM131221P00029000"
), class = "factor"), Last = c(1.05, 2, 3.2, 4.3), Chg = c(0, 
0, 0, 0), Bid = c(1, 1.45, 1.95, 3.8), Ask = c(1.4, 1.85, 2.35, 
4.4), Vol = c(20L, 1L, 10L, 10L), Open.Int = c(20L, 5L, 10L, 
10L)), .Names = c("Strike", "Symbol", "Last", "Chg", "Bid", "Ask", 
"Vol", "Open.Int"), row.names = c(NA, -4L), class = "data.frame"), 
    Stock.ticker = "VIXM", Quote.date = <S4 object of class structure("timeDate", package = "timeDate")>, 
    Strike.date = <S4 object of class structure("timeDate", package = "timeDate")>, 
    Stock.name = "ProShares VIX Mid-Term Futures ETF (VIXM)", 
    Stock.price = 26.34, TTM = 193, Short.rate = 0.0893939393939394), .Names = c("call", 
"put", "Stock.ticker", "Quote.date", "Strike.date", "Stock.name", 
"Stock.price", "TTM", "Short.rate"))

提取上述信息的雅虎财务页面是:

http://finance.yahoo.com/q/op?s=VIXM&m=2013-12

我想从上面的变量OptionPrices中为罢工,期权符号,买入价,卖出价等创建向量。

我如何实现这一目标。

1 个答案:

答案 0 :(得分:2)

即使我加载了R包timeDate,我也无法使您的代码正常工作。 但是,一旦我摆脱了Stock.dateQuote.date部分,我就能看到你所得到的东西。

OptionPrices <- structure(list(call = structure(list(Strike = 26, Symbol = structure(1L, .Label = "VIXM131221C00026000", class = "factor"), 
    Last = 1.8, Chg = 0, Bid = 2.05, Ask = 2.65, Vol = 10L, Open.Int = 10L), .Names = c("Strike", 
    "Symbol", "Last", "Chg", "Bid", "Ask", "Vol", "Open.Int"), row.names = c(NA, 
    -1L), class = "data.frame"), put = structure(list(Strike = c(24, 
    25, 26, 29), Symbol = structure(1:4, .Label = c("VIXM131221P00024000", 
    "VIXM131221P00025000", "VIXM131221P00026000", "VIXM131221P00029000"
    ), class = "factor"), Last = c(1.05, 2, 3.2, 4.3), Chg = c(0, 
    0, 0, 0), Bid = c(1, 1.45, 1.95, 3.8), Ask = c(1.4, 1.85, 2.35, 
    4.4), Vol = c(20L, 1L, 10L, 10L), Open.Int = c(20L, 5L, 10L, 
    10L)), .Names = c("Strike", "Symbol", "Last", "Chg", "Bid", "Ask", 
    "Vol", "Open.Int"), row.names = c(NA, -4L), class = "data.frame"), 
    Stock.ticker = "VIXM", Stock.name = "ProShares VIX Mid-Term Futures ETF (VIXM)", 
    Stock.price = 26.34, TTM = 193, Short.rate = 0.0893939393939394), .Names = c("call", 
    "put", "Strike.date", "Stock.name", 
    "Stock.price", "TTM", "Short.rate"))

因此,OptionPrices是一个列表,您似乎对put元素中包含的信息感兴趣。 您可以将此元素保存到数据框,然后使用名称或列号访问数据框的每个元素(向量)。

df <- OptionPrices$put
df$Last
df[, 3]