So etwas wie expand.grid auf einer Liste von Listen
Ich habe drei Textdokumente als Liste von Listen mit dem Namen "dlist" gespeichert:
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"))
In meinem Kopf finde ich es hilfreich, eine D-Liste so abzubilden:
name text
1 a c("the", "quick", "brown")
2 b c("fox", "jumps", "over", "the")
3 c c("lazy", "dog")
Wie kann dies wie folgt manipuliert werden? Die Idee ist, es grafisch darzustellen, also wäre etwas, das für ggplot2 geschmolzen werden kann, gut.
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
Das ist eine Zeile pro Wort und gibt sowohl das Wort als auch das übergeordnete Dokument an.
Ich habe versucht:
> 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"
Es ist fair zu sagen, ich bin verwirrt von den verschiedenen Apply- und Plyr-Funktionen und weiß nicht wirklich, wo ich anfangen soll. Ich habe noch nie ein Ergebnis wie im oben genannten Versuch "sapply" gesehen und verstehe es nicht.