1

Given the following unequal list :

lst <- list("es1-7"= c(1,2,3,4), "sa1-12"=c(3,4) , "ES8-13"= c(9,7,4,1,5,2))

> lst
$`es1-7`
[1] 1 2 3 4

$`sa1-12`
[1] 3 4

$`ES8-13`
[1] 9 7 4 1 5 2

I would like to create a data-frame like this:

 group     numbers
1  es1-7       1
2  es1-7       2
3  es1-7       3
4  es1-7       4
5 sa1-12       3
6    ...     ...

So in this case names of the list will be values of a new column called groupand numbers will be the values of the list.

Solutions using base and dplyr are more than welcome

moth
  • 1,833
  • 12
  • 29
  • 2
    Possible duplicate of [Named List To/From Data.Frame](https://stackoverflow.com/questions/10432993/named-list-to-from-data-frame) – camille Aug 21 '19 at 19:15

1 Answers1

2

We can use stack from base R to create a two column data.frame

stack(lst)[2:1]

Or with enframe

library(tidyverse)
enframe(lst, name = "group", value = "numbers") %>%
    unnest
akrun
  • 874,273
  • 37
  • 540
  • 662