Re: [R] collapsing list to data.frame

From: Dimitris Rizopoulos <Dimitris.Rizopoulos_at_med.kuleuven.be>
Date: Wed, 05 Mar 2008 21:28:59 +0100

try this:

l <- vector("list", 3)

l[[1]] <- list(4, "hello")
l[[2]] <- list(7, "world")
l[[3]] <- list(9, " !!!! ")

lis <- lapply(l, "names<-", value = c("V1", "V2")) do.call("rbind", lapply(lis, data.frame, stringsAsFactors = FALSE))

I hope it helps.

Best,
Dimitris



Dimitris Rizopoulos
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium

Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
      http://www.student.kuleuven.be/~m0390867/dimitris.htm


Quoting a9804814_at_unet.univie.ac.at:

> Hello,
>
>
> Given a list with all elements having identical layout, e.g.:
>
>
> l = NULL
> l[[1]] = list(4, "hello")
> l[[2]] = list(7, "world")
> l[[3]] = list(9, " !!!! ")
>
>
> is there an easy way to collapse this list into a data.frame with each
> row being the elements of the list ?
> I.e. in this case I want to convert the list into a data.frame with 3
> rows and 2 columns, where column 1 holds the integer values, and column
> 2 the character values.
>
> I can get it done by looping over all elements and rbind them together
> to the final result, but that is quite slow (for large sets) and ugly,
> so I was wondering if there's an easy syntax.
>
> thanks a lot in advance,
> Thomas
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 05 Mar 2008 - 20:39:59 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 05 Mar 2008 - 21:30:20 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive