Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert this list to a dataframe with same names in R?

Tags:

dataframe

r

I have the following data in R:

list0 <- list(ff = 45,gg = 23)
list1 <- list(a = 2, b=list0)
LIST <- list(mylist = list1)

I want to convert this list to a dataframe and get an output dataframe as follows, which has the following column header naming conventions:

  a b.ff b.gg
1 2   45   23

any help is appreciated.

like image 979
user121 Avatar asked Dec 12 '25 14:12

user121


2 Answers

The LIST step was unnecessary:

> data.frame(list1)
  a b.ff b.gg
1 2   45   23
like image 121
lebelinoz Avatar answered Dec 14 '25 04:12

lebelinoz


vec <- unlist(LIST)
names(vec) <- sub("mylist.", "", names(vec))
dt <- data.frame(as.list(vec))

dt
  a b.ff b.gg
1 2   45   23
like image 44
www Avatar answered Dec 14 '25 03:12

www



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!