2017-02-28 31 views
1

我試圖獲取列表的索引,例如set_of_paragraphs("Two sample t-test",list_new2[[1]], list_new2[[2]],list_new2[[3]],list_new2[[4]]) ...等等。如何獲取列表索引的向量R

library(ReporteRs) 
list_new <- c("Text1","Text2","String","Another Text") 
my_text <- letters[1:length(list_new)] 
list_new1 <- paste0(my_text, list_new,sep="") 
list_new2 <- lapply(list_new1, function(i) pot(substr(i,1,1),textProperties(color="blue",vertical.align="superscript"))+substring(i,2)) 

功能只有當我列出所有指數在列表set_of_paragraphs工作

set_of_paragraphs("Two sample t-test",list_new2[[1]], list_new2[[2]],list_new2[[3]],list_new2[[4]]) 

我嘗試做這種方式,set_of_paragraphs給了我錯誤

Error in set_of_paragraphs(l, list_new2) : set_of_paragraphs can only contains pot objects.

l <- list("Two sample t-test") 
set_of_paragraphs(l,list_new2) 

因此,我最好的方法是將它們全部列入代碼set_of_paragraphs("Two sample t-test",list_new2[[1]], list_new2[[2]],list_new2[[3]],list_new2[[4]]),但問題是,我有這麼多,有什麼辦法可以寫循環或者申請訪問索引。

回答

2

如果要使用參數列表調用函數,可以使用do.call。嘗試

l <- list("Two sample t-test") 
do.call("set_of_paragraphs" c(l, list_new2)) 

這是

set_of_paragraphs(l[[1], list_new2[[1]], list_new2[[2]], list_new2[[3]], ...) 

等效(我不能測試,因爲那個包似乎需要Java,我沒有安裝。)基本上,你把所有的參數變成一個大名單(這裏我使用c()來加入兩個列表)。

+0

是的,它完美的作品 – BIN