Named List To/From Data.Frame
Another option is enframe
from tibble
library(tidyverse)
enframe(lst1) %>%
unnest
data
lst1 <- list(a=1:3, b=4:6)
Use stack
and unstack
in base R:
x <- data.frame(a=1:3, b=4:6)
x
a b
1 1 4
2 2 5
3 3 6
Use stack
to from wide to tall, i.e. stack the vectors on top of one another.
y <- stack(x)
y
values ind
1 1 a
2 2 a
3 3 a
4 4 b
5 5 b
6 6 b
Use unstack
to do the reverse.
unstack(y)
a b
1 1 4
2 2 5
3 3 6
If your data structure is more complicated than you described, stack
and unstack
may no longer be suitable. In that case you'll have to use reshape
in base R, or melt
and dcast
in package reshape2
.