Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a list into separate data frame in R

So I have a list with me as below, what I want is to split them into three separate dataframes (with names as Banana/Strawberry & apple) as shown in expected output. I have already seen this (Splitting List into dataframe R) but its exact opposite of what I want. I dont want to combine then I want to split them into three dataframe with same name as list header.

list_a <- list(`Banana` = c(8.7), `Strawberry` = c(2.3), `Apple` = c(3.5))

DF1

Banana
8.7

DF2

Strawberry
2.3

DF3

Apple
3.5

Any Solution preferably in Tidyverse would be greatly appreciated. Actual problem has lot more columns in the list.

like image 550
Vaibhav Singh Avatar asked Dec 04 '22 17:12

Vaibhav Singh


1 Answers

First convert them all to a tibble:

list_a <- list(`Banana` = c(8.7), `Strawberry` = c(2.3), `Apple` = c(3.5))
list_a <- purrr::map(list_a, tibble::as_tibble)

Then send this to the global environment:

list2env(list_a, envir = .GlobalEnv)
like image 121
al-obrien Avatar answered Dec 11 '22 15:12

al-obrien