2013-05-01 89 views
14

我有存儲爲所謂的「DLIST」列出的清單三個文本文檔的列表上:喜歡的東西expand.grid名單

dlist <- structure(list(name = c("a", "b", "c"), text = list(c("the", "quick", "brown"), c("fox", "jumps", "over", "the"), c("lazy", "dog"))), .Names = c("name", "text")) 

在我腦子裏,我發現它有助於圖片DLIST是這樣的:

name text 
1 a  c("the", "quick", "brown") 
2 b  c("fox", "jumps", "over", "the") 
3 c  c("lazy", "dog") 

這怎麼能被操縱如下?這個想法是繪製它,所以可以熔化ggplot2的東西會很好。

name text 
1 a the 
2 a quick 
3 a brown 
4 b fox 
5 b jumps 
6 b over 
7 b the 
8 c lazy 
9 c dog 

這是每個單詞一行,同時提供單詞和其父文檔。

我曾嘗試:

> expand.grid(dlist) 
    name     text 
1 a  the, quick, brown 
2 b  the, quick, brown 
3 c  the, quick, brown 
4 a fox, jumps, over, the 
5 b fox, jumps, over, the 
6 c fox, jumps, over, the 
7 a    lazy, dog 
8 b    lazy, dog 
9 c    lazy, dog 

> sapply(seq(1,3), function(x) (expand.grid(dlist$name[[x]], dlist$text[[x]]))) 
    [,1]  [,2]  [,3]  
Var1 factor,3 factor,4 factor,2 
Var2 factor,3 factor,4 factor,2 

unlist(dlist) 
    name1 name2 name3 text1 text2 text3 text4 
    "a"  "b"  "c" "the" "quick" "brown" "fox" 
    text5 text6 text7 text8 text9 
"jumps" "over" "the" "lazy" "dog" 

> sapply(seq(1,3), function(x) (cbind(dlist$name[[x]], dlist$text[[x]]))) 
[[1]] 
    [,1] [,2] 
[1,] "a" "the" 
[2,] "a" "quick" 
[3,] "a" "brown" 

[[2]] 
    [,1] [,2] 
[1,] "b" "fox" 
[2,] "b" "jumps" 
[3,] "b" "over" 
[4,] "b" "the" 

[[3]] 
    [,1] [,2] 
[1,] "c" "lazy" 
[2,] "c" "dog" 

公平地說,我通過各種迷惑申請和plyr功能,真的不知道從哪裏開始。我從來沒有見過類似於上面的「僥倖」嘗試的結果,並且不理解它。

+1

您可以更緊密地格式化到你在你的腦袋是什麼,就像這樣:'DLIST <-list(A = C(下稱「」,「快」, 「褐色」),...)'。這樣做也可以簡化對這個問題的答案。 – Frank 2013-05-01 21:09:52

+0

謝謝Frank,Josh的setNames函數告訴我如何去做。 – nacnudus 2013-05-01 21:36:50

回答

11

如果您將dlist轉換爲命名列表(我認爲更適合的結構),則可以使用stack()來獲取所需的兩列數據。

(該rev()setNames()在第二行調用只是衆多方法來調整列的順序和名稱以匹配你的問題出所需的輸出之一。)

x <- setNames(dlist$text, dlist$name) 
setNames(rev(stack(x)), c("name", "text")) 
# name text 
# 1 a the 
# 2 a quick 
# 3 a brown 
# 4 b fox 
# 5 b jumps 
# 6 b over 
# 7 b the 
# 8 c lazy 
# 9 c dog 
+1

+1我有*不知道這是如何工作的。現在我明白了,我喜歡那樣。 – 2013-05-01 21:32:23

+0

感謝三個偉大的新功能,特別是setNames,這意味着我可以在事後跟蹤Frank的評論,而不是直接回到開頭。 – nacnudus 2013-05-01 21:34:50

+0

@ SimonO101 - 噢好。起初,我實際上已經拒絕發佈這個消息,因爲它在幾行中包含了很多步驟。根據你和nacnudus的評論,雖然,我很高興我做到了。 (FWIW,我可能*真的*使用'(dlist,setNames(text,name))',我。) – 2013-05-01 21:54:25

0

Josh的答案是更甜但我想我會把我的帽子扔在戒指裏。

dlist <- structure(list(name = c("a", "b", "c"), 
    text = list(c("the", "quick", "brown"), 
    c("fox", "jumps", "over", "the"), c("lazy", "dog"))), 
    .Names = c("name", "text")) 

lens <- sapply(unlist(dlist[-1], recursive = FALSE), length) 

data.frame(name = rep(dlist[[1]], lens), text = unlist(dlist[-1]), row.names = NULL) 

## name text 
## 1 a the 
## 2 a quick 
## 3 a brown 
## 4 b fox 
## 5 b jumps 
## 6 b over 
## 7 b the 
## 8 c lazy 
## 9 c dog 

這就是說,列表清單是一種尷尬的存儲方法。向量列表(特別是向量列表)將更容易處理。

1

另一種解決方案,也許更普及:

do.call(rbind, do.call(mapply, c(dlist, FUN = data.frame, SIMPLIFY = FALSE))) 

#  name text 
# a.1 a the 
# a.2 a quick 
# a.3 a brown 
# b.1 b fox 
# b.2 b jumps 
# b.3 b over 
# b.4 b the 
# c.1 c lazy 
# c.2 c dog 
+0

這比Simon O'Hanlon的建議更好,因爲它允許多列的數據幀(比如「名稱「)在基於列表列的基礎上擴展爲行! – datamole 2015-09-01 13:26:49

相關問題