I do not know about the most effective ones, but using your list:
l <- list(a=c("a1","a2"), b=c("b1","b2"), c="c1")
we can get the length of each component with sapply()
lens <- sapply(l, length)
we just l lensretype the names a number of times and unlist l- here it is executed on one line:
cbind(rep(names(l), times = sapply(l, length)), unlist(l))
which gives the desired result:
R> cbind(rep(names(l), times = sapply(l, length)), unlist(l))
[,1] [,2]
a1 "a" "a1"
a2 "a" "a2"
b1 "b" "b1"
b2 "b" "b2"
c "c" "c1"
source
share