I would like to convert an R data.frame containing a list of uneven length to long format as in the example below.
testdf <- data.frame(a=1:3,b=I(list(letters[1:2],letters[2:7],letters[1:3])))
tesdf
a b
1 1 a, b
2 2 b, c, d,....
3 3 a, b, c
the resulting format should look like this
a b
1 1 a
2 1 b
3 2 b
4 2 c
5 2 d
6 2 e
7 2 f
8 2 g
9 3 a
10 3 b
11 3 c
A for-loop-way to achieve this would look something like this
resultdf <- data.frame()
for(i in as.numeric(row.names(testdf))){resultdf <- rbind(resultdf, data.frame(a=testdf$a[i],b=unlist(testdf$b[i][[1]])))}
however this proves to be very slow and I need to to this for a large data.frame (~6mio rows and an average length of list of 10 items). I tried melt as follows
library(reshape2)
> melt(test, id.var="a", value.var="b")
Error: Can't melt data.frames with non-atomic 'measure' columns
but I'm not even sure if melt is meant to work with lists. Which would be the fastest was to do this?
btw.: I produce the initial data.frame by using str_extract_all()