Re: [R] converting a list to a data.frame

From: Mark Wardle <mark_at_wardle.org>
Date: Tue 03 Apr 2007 - 11:35:21 GMT

Biscarini, Filippo wrote:
> Dear Mark,
>
> If you have time you can try directly into R this excerpt from my list.
>

This is horrible code, and I should be shot! I would appreciate any better ways of doing this, but I think this works!

l = list(c(1,2,4),c(4,5,2,1),c(3,4,6,3),c(3,1,76,4,2))

fix.it <- function(list) {

        max.length = 0
        x = NULL
		for(i in list) max.length = max(max.length, length(i))
		for(i in list) {
        	d = c(i, rep(NA, max.length - length(i)))
        	if(is.null(x)) x = rbind(unlist(d))
        	else x = rbind(x, unlist(d))
		}
		return(x)

}

fix.it(l)

It works on the data you sent me:

> fix.it(A)

Have fun,

Mark

-- 
Dr. Mark Wardle
Specialist registrar, Neurology
Cardiff, UK

______________________________________________
R-help@stat.math.ethz.ch 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 Tue Apr 03 21:40:48 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 03 Apr 2007 - 12:30:48 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.