I have three text documents stored as a list of lists called \"dlist\":
dlist <- structure(list(name = c(\"a\", \"b\", \"c\"), text = list(c(\"the\", \"quick\
Another solution, maybe more generalizable:
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
If you convert your dlist
to a named list (a better suited structure in my opinion), you can use stack()
to get the two column data.frame you want.
(The rev()
and setNames()
calls in the second line are just one of many ways to adjust the column ordering and names to match the desired output shown in your question.)
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
Josh's answer is much sweeter but I thought I'd throw my hat in the ring.
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
That being said the list of lists is a bit of an awkward storage method. A list of vectors (particularly named lists of vectors) would be easier to deal with.