[R] converting a list to a data.frame

Mark Wardle mark at wardle.org
Tue Apr 3 13:35:21 CEST 2007

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))


It works on the data you sent me:

> fix.it(A)

Have fun,


Dr. Mark Wardle
Specialist registrar, Neurology
Cardiff, UK

More information about the R-help mailing list