2016-11-15 106 views
1

我有嵌套的列表數據需要在所需的輸出表示,或者矩陣像對象或只是直接導出這些嵌套列表爲csv文件。我嘗試了幾種通用的方法來完成這項任務,但是導出嵌套列表的方式並不順利,所以我正在尋找將嵌套列表轉換爲矩陣或像表格一樣的解決方案,以便以期望的方式保存數據。也許我可以在data.table中保存嵌套列表數據,但對此不太確定。誰能告訴我如何輕鬆做這種操作?如何爲嵌套列表數據實現乾淨,結構良好的數據表示?任何想法 ?非常感謝如何將嵌套列表轉換爲矩陣狀或表格狀對象?

迷你例如:

自定義函數的輸出:

AcceptedList <- list(
    A_accepted = data.frame(pos.start=c(1,6,16), pos.stop=c(4,12,23), pos.ID=c("A1","A2","A3"), pos.score=c(11,8,13)), 
    B_accepted = data.frame(pos.start=c(7,19,31), pos.stop=c(13,28,43), pos.ID=c("B3","B6","B7"), pos.score=c(12,5,7)), 
    C_accepted = data.frame(pos.start=c(5,21,36), pos.stop=c(11,29,42), pos.ID=c("C2","C4","C9"), pos.score=c(7,13,9)) 
) 

RejectedList <- list(
    A_rejected = data.frame(pos.start=c(6,25,40), pos.stop=c(12,33,49), pos.ID=c("A2","A5","A8"), pos.score=c(8,4,7)), 
    B_rejected = data.frame(pos.start=c(15,19,47), pos.stop=c(18,28,55), pos.ID=c("B4","B6","B9"), pos.score=c(10,5,14)), 
    C_rejected = data.frame(pos.start=c(13,21,36,53), pos.stop=c(19,29,42,67), pos.ID=c("C3","C4","C9","C12"), pos.score=c(4,13,9,17)) 
) 

讓我實現這個功能,以進一步控制輸出的一個步驟:

func <- function(mlist, threshold) { 
    res <- lapply(mlist, function(x) { 
    splt <- split(x, ifelse(x$pos.score >= threshold, "up", "down")) 
    }) 
    return(res) 
} 

#example 
.res_accepted <- func(AcceptedList, 9) 
.res_rejected <- func(RejectedList, 9) 

我有困難時期如何將嵌套列表.res_accepted, .res_rejected作爲矩陣狀對象。理想情況下,將嵌套列表導出爲csv文件非常值得期待,但我未能以期望的方式導出它們。我怎樣才能做到這一點?

最終,以期望命名爲CSV文件所需列表如下:

A_accepted_up.csv 
A_accepted_down.csv 
A_rejected_up.csv 
A_rejected_down.csv 
B_accepted_up.csv 
B_accepted_down.csv 
B_rejected_up.csv 
B_rejected_down.csv 
C_accepted_up.csv 
C_accepted_down.csv 
C_rejected_up.csv 
C_rejected_down.csv 

的一點是,通過我的自定義函數返回嵌套列表,所以我打算直接出口或將它們扔在像矩陣對象也是如此。任何想法這種操縱?謝謝:)

回答

4

這將返回數據的data.frame DF。沒有包被使用。

both <- do.call("rbind", c(AcceptedList, RejectedList)) 
cn <- c("letter", "accepted", "seq") 
DF <- cbind(
     read.table(text = chartr("_", ".", rownames(both)), sep = ".", col.names = cn), 
     both) 
DF <- transform(DF, updown = ifelse(pos.score > 8, "up", "down")) 

,並提供:

> DF 
      letter accepted seq pos.start pos.stop pos.ID pos.score updown 
A_accepted.1  A accepted 1   1  4  A1  11  up 
A_accepted.2  A accepted 2   6  12  A2   8 down 
A_accepted.3  A accepted 3  16  23  A3  13  up 
B_accepted.1  B accepted 1   7  13  B3  12  up 
B_accepted.2  B accepted 2  19  28  B6   5 down 
B_accepted.3  B accepted 3  31  43  B7   7 down 
C_accepted.1  C accepted 1   5  11  C2   7 down 
C_accepted.2  C accepted 2  21  29  C4  13  up 
C_accepted.3  C accepted 3  36  42  C9   9  up 
A_rejected.1  A rejected 1   6  12  A2   8 down 
A_rejected.2  A rejected 2  25  33  A5   4 down 
A_rejected.3  A rejected 3  40  49  A8   7 down 
B_rejected.1  B rejected 1  15  18  B4  10  up 
B_rejected.2  B rejected 2  19  28  B6   5 down 
B_rejected.3  B rejected 3  47  55  B9  14  up 
C_rejected.1  C rejected 1  13  19  C3   4 down 
C_rejected.2  C rejected 2  21  29  C4  13  up 
C_rejected.3  C rejected 3  36  42  C9   9  up 
C_rejected.4  C rejected 4  53  67 C12  17  up 

這將寫DF出在不同的文件:

junk <- by(DF, DF[c("letter", "accepted", "updown")], 
     function(x) write.csv(x[-(1:3)], 
     sprintf("%s_%s_%s.csv", x$letter[1], x$accepted[1], x$updown[1]))) 

或本會寫出來的數據幀.res_accepted - .res_rejected可以類似地處理:

junk <- lapply(names(.res_accepted), function(nm) 
    mapply(write.csv, 
      .res_accepted[[nm]], 
      paste0(nm, "_", names(.res_accepted[[nm]]), ".csv"))) 

說明:海報在此答案已經出現後更改了數據。上面的輸出對應於原始數據;但是,它也應該用於修改後的數據。原始數據爲:

AcceptedList <- 
structure(list(foo_accepted = structure(list(pos.start = c(1, 
6, 16), pos.stop = c(4, 12, 23), pos.ID = structure(1:3, .Label = c("A1", 
"A2", "A3"), class = "factor"), pos.score = c(11, 8, 13)), .Names = c("pos.start", 
"pos.stop", "pos.ID", "pos.score"), row.names = c(NA, -3L), class = "data.frame"), 
    bar_accepted = structure(list(pos.start = c(7, 19, 31), pos.stop = c(13, 
    28, 43), pos.ID = structure(1:3, .Label = c("B3", "B6", "B7" 
    ), class = "factor"), pos.score = c(12, 5, 7)), .Names = c("pos.start", 
    "pos.stop", "pos.ID", "pos.score"), row.names = c(NA, -3L 
    ), class = "data.frame"), cat_accepted = structure(list(pos.start = c(5, 
    21, 36), pos.stop = c(11, 29, 42), pos.ID = structure(1:3, .Label = c("C2", 
    "C4", "C9"), class = "factor"), pos.score = c(7, 13, 9)), .Names = c("pos.start", 
    "pos.stop", "pos.ID", "pos.score"), row.names = c(NA, -3L 
    ), class = "data.frame")), .Names = c("foo_accepted", "bar_accepted", 
"cat_accepted")) 

RejectedList <- 
structure(list(foo_rejected = structure(list(pos.start = c(6, 
25, 40), pos.stop = c(12, 33, 49), pos.ID = structure(1:3, .Label = c("A2", 
"A5", "A8"), class = "factor"), pos.score = c(8, 4, 7)), .Names = c("pos.start", 
"pos.stop", "pos.ID", "pos.score"), row.names = c(NA, -3L), class = "data.frame"), 
    bar_rejected = structure(list(pos.start = c(15, 19, 47), 
     pos.stop = c(18, 28, 55), pos.ID = structure(1:3, .Label = c("B4", 
     "B6", "B9"), class = "factor"), pos.score = c(10, 5, 
     14)), .Names = c("pos.start", "pos.stop", "pos.ID", "pos.score" 
    ), row.names = c(NA, -3L), class = "data.frame"), cat_rejected = structure(list(
     pos.start = c(13, 21, 36, 53), pos.stop = c(19, 29, 42, 
     67), pos.ID = structure(c(2L, 3L, 4L, 1L), .Label = c("C12", 
     "C3", "C4", "C9"), class = "factor"), pos.score = c(4, 
     13, 9, 17)), .Names = c("pos.start", "pos.stop", "pos.ID", 
    "pos.score"), row.names = c(NA, -4L), class = "data.frame")), 
.Names = c("foo_rejected", 
"bar_rejected", "cat_rejected")) 
+0

非常感謝。這正是我想要做的。非常感謝:) – Jerry

+0

我想通了的作用。再次感謝您的貢獻:) – Jerry