2013-11-03 68 views
1

edit第一如何正確使用省略號嵌套函數

我有一個父功能和子功能。我想在我的孩子功能中使用...功能。具體來說,我想知道怎樣才能正確地使用這些代碼特定的行...在我的孩子功能:

function(theList,...){ 

    #### 
    CombinedList=lapply(seq_along(theList), function (x,...) { 

    .x <- as.list(substitute(list(...)))[-1] 

    elementz=data.frame(rbind(.x[1][[x]],.x[2][[x]],.x[3][[x]])) 

...應該是列表的列表,即...列表中的每個元素是一個列表。 在我的主(主)功能下,...將包含SharpListSortinoList

windowSize=36 

    PerformanceDF <- function(data,windowSize,AvgForvalt=(0.02/12)){ 
     require(quantmod,quietly = TRUE) 
     require(reshape2,quietly = TRUE) 
     require(PerformanceAnalytics,quietly = TRUE) 
     require(xts,quietly = TRUE) 

     #36 mån rolling window list 

     windows <- embed(1:nrow(data), windowSize) 

     lapplyApproach <- function(data, windows) { 
     windowsList <- split(t(windows), rep(1:nrow(windows), each=ncol(windows))) 
     names(windowsList) <- unlist(lapply(windowsList, 
              function(x) data[x[1],1])) 
     return(lapply(windowsList,function(x) data[rev(x),])) 
     } 


     DropBench=grep("Benchmark",names(hedgesample),ignore.case=TRUE) 
     data=data[,-c(DropBench)] 

     theList=lapplyApproach(data,windows) 


     ##xts 

     theListXts=lapply(theList,function(x){ 

     blab=xts(x[,-1],as.Date(x[,1])) 

     return(blab) 

     }) 



     ##Sharpe Ratio  
     SharpList=lapply(theListXts,function(x) { 

     hej=SharpeRatio(x,FUN="StdDev") 

     rownames(hej)=NULL 

     hej[is.infinite(hej)] <- 0 

     return(hej) 

     }) 

     ##Sortino ratio 

     SortinoList=lapply(theListXts,function(x) { 

     mmm=SortinoRatio(x) 

     rownames(mmm)=NULL 

     mmm[is.infinite(mmm)]<-0 

     return(mmm) 

     }) 




     function(theList,...){ 




     #### 
     CombinedList=lapply(seq_along(theList), function (x,...) { 

     .x <- as.list(substitute(list(...)))[-1] 

     elementz=data.frame(rbind(.x[1][[x]],.x[2][[x]],.x[3][[x]])) 

     rownames(elementz)=NULL 

     elementz$Statistic=.x 

     return(elementz) 


     },...) 

     names(CombinedList)=names(theList) 

     return(CombinedList)}} 

現在我得到的error

Error in .x[1][[x]] : subscript out of bounds 
In addition: Warning messages: 
1: In rbind(.x[1][[x]], .x[2][[x]]) : 
    (symbol) object cannot be coerced to type 'list' 
2: In rbind(.x[1][[x]], .x[2][[x]]) : 
    (symbol) object cannot be coerced to type 'list' 
3: In rbind(.x[1][[x]], .x[2][[x]]) : 
    (symbol) object cannot be coerced to type 'list' 

祝商祺!

EDIT

找到了解決辦法:

function(theList,...){ 

    CombinedList=lapply(seq_along(theList), function (x,...) { 

    .x <- as.list(substitute(list(...)))[-1] 

    elementz=data.frame(rbind(eval(.x[[1]])[[x]],eval(.x[[2]])[[x]],eval(.x[[3]])[[x]])) 

但沒有任何人有一個更好的解決方案嗎?

+0

請參閱'?do.call'。 – fotNelton

+0

@fNelton Well ...找到解決方案CombinedList = lapply(seq_along(theList),function(x,...){ .x < - as.list(substitute(list(...)))[ - 1] elementz = data.frame(rbind(eval(.x [[1]])[[x]],eval(.x [[2]])[[x]],eval(.x [ 3])[[X])) rownames(elementz)= NULL elementz $統計= as.character(.X) 回報(elementz) },...) – user1665355

+0

請張貼它作爲回答而不是評論。 – zero323

回答

0
function(theList,...){ 

    CombinedList=lapply(seq_along(theList), function (x,...) { 

    .x <- as.list(substitute(list(...)))[-1] 

    elementz=data.frame(rbind(eval(.x[[1]])[[x]],eval(.x[[2]])[[x]],eval(.x[[3]])[[x]])) 
相關問題