convert list to tibble

i have a list and i want to convert list to tibble.

l <- list(list(a = 1,b =  2, c = 3, d = 4), list(a = "a", b = "b", c  =  "c",  d = "d"), list(a = 5, b = 6, c = 7, d = 8))

but i want to my tibble get column names similar names my list (a,b,c,d). how can convert it?

Hi, your list contains 3 lists, how do they relate to each other ?
I have to assume you want columns a,b,c,d but you have provided both numbers and characters to populate them, a tibble needs a column to have a single type. I will therefore have to assume that you mean characters, because i cant represent the 'a','b','c','d' you declared as numbers.

library(tidyverse)

l <- list(list(a = 1,b =  2, c = 3, d = 4), list(a = "a", b = "b", c  =  "c",  d = "d"), list(a = 5, b = 6, c = 7, d = 8))
l2 <- map(l,~data.frame(.))
l3 <-map_dfr(l2,~mutate_all(.,as.character))  
l4<-as_tibble(l3)
# A tibble: 3 x 4
  a     b     c     d    
  <chr> <chr> <chr> <chr>
1 1     2     3     4    
2 a     b     c     d    
3 5     6     7     8
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.