I would like to combine a list of character vectors into a single data frame. For example, I want
list(A=c("123","456"), B=c("789"), C=c("000"))
to become:
label val
1 A 123
2 A 456
3 B 789
4 C 000
I came up with the following. It works, but it seems clunky. Is there a more elegant way to do the above?
l <- list(A=c("123","456"), B=c("789"), C=c("000"))
res <- lapply(names(l), function(x) {
data.frame(label=rep(x, length(l[[x]])), val=l[[x]],
stringsAsFactors = F) })
do.call("rbind", res)