ModalBro
ModalBro

Reputation: 554

Turning a list with varying vector lengths into a dataframe in R

I'm trying to convert a named list of vectors with varying lengths into a dataframe so that I can plot it later. Right now, I have a list that looks like this:

list <- list(a=c("foo"), b=c("foo","foo"), c=c("foo", "foo", "bar"))

What I'd like is a dataframe that looks like this:

letters=c("a","b","b","c","c","c") 
text=c("foo","foo","foo","foo", "foo", "bar")
df <- data.frame(letters,text)

I realize this is probably pretty simple, but I just can't seem to wrap my head around it. Thank you all for your help!

Upvotes: 3

Views: 84

Answers (1)

akrun
akrun

Reputation: 887048

We can use stack or melt

stack(list)

Or

library(reshape2)
melt(list)

Upvotes: 4

Related Questions