I have a list which can have either empty entries, entries containing one elements and entries containing multiple elements.
l1 = list(integer(0), 11L, integer(
A "low level" solution:
data.frame(entry=rep(seq_along(l1),lengths(l1)),element=unlist(l1))
# entry element
#1 2 11
#2 5 11
#3 6 11
#4 7 6
#5 7 36
#6 8 16
#7 9 16
We can have two options. Make the list
a named one, enframe
it to a tbl_df
and then unnest
the list
element. The NULL
elements will be automatically removed
library(tidyverse)
l1 %>%
set_names(seq_along(.)) %>%
enframe %>%
unnest
Or after naming the list
, stack
it to a 2 column data.frame
stack(setNames(l1, seq_along(l1)))[2:1]