2013-02-06 46 views
0

列表的stucture問題我有創建計算不同統計信息的列表如下的函數:與限定中的R

RiskStats = list(stdev,sharpe,downdev,sortino,maxDD,calmar,currDD) 

> RiskStats 
[[1]] 
      Index 
StdDev  3.2506 

[[2]] 
           Index 
Annualized Sharpe Ratio (Rf=0%)  -1.2434 

[[3]] 
            Index 
Downside Deviation (MAR = 0%)  3.194 

[[4]] 
           Index 
Sortino Ratio (MAR = 0%)  -1.2655 

[[5]] 
      Index 
maxDD  -8.9467 

[[6]] 
        Index 
Calmar Ratio  -0.44846 

[[7]] 
       Index 
Current DD  -8.543 

我想設置此列表的結構(7行和1列),我們可以簡單的寫

dim(RiskStats) <- c(7,1) 

爲什麼我們得到了一定的成績,結果[列表1],而正確的統計計算的?

> RiskStats 
        Index 
Annual volatility 3.2506  
Sharpe ratio  -1.2434  
Downside deviation 3.194  
Sortino ratio  -1.2655  
Maximum Drawdown List,1  
Calmar ratio  -0.44846  
Current drawdown List,1 
+3

我想你可能在尋找'do.call(「rbind」,RiskStats)''。 –

+0

這樣做的工作。謝謝! –

回答

1

使用此數據

dput(RiskStats) 
RiskStats <- list(structure(list(Index = 3.2506), .Names = "Index", row.names = "StdDev", class = "data.frame"), 
    structure(list(Index = -1.2434), .Names = "Index", row.names = "Annualized_Sharpe_Ratio_Rf", class = "data.frame"), 
    structure(list(Index = 3.194), .Names = "Index", row.names = "Downside_Deviation_MAR_0", class = "data.frame"), 
    structure(list(Index = -8.9467), .Names = "Index", row.names = "maxDD", class = "data.frame"), 
    structure(list(Index = -0.44846), .Names = "Index", row.names = "Calmar_Ratio", class = "data.frame"), 
    structure(list(Index = -8.543), .Names = "Index", row.names = "Current_DD", class = "data.frame")) 

您可以通過多種方式實現自己的目標,下面是其中一些:

# Alternative 1 (suggested by @Roman Luštrik) 
do.call(rbind, RiskStats) # I think this is the best one 

# Alternative 2 
Reduce(rbind, RiskStats) 

# Alternative 3 
t(data.frame(lapply(RiskStats, t))) # It's not so good, but it works. 

無論替代你選擇,你最終會得到下面的結果:

       Index 
StdDev      3.25060 
Annualized_Sharpe_Ratio_Rf -1.24340 
Downside_Deviation_MAR_0 3.19400 
maxDD      -8.94670 
Calmar_Ratio    -0.44846 
Current_DD     -8.54300 
0

原因是因爲壓縮和最大壓縮是列表本身,其他人可能會列出其他一些數據結構。

嘗試unlist()這些元素。