Hello,

There's nothing inelegant in your solution, you're using vectorized instructions.
But there's a bug. The argument to rep should be 'each', not 'times'.

l2$name <- rep(names(l), each=sapply(l,nrow))

Hope this helps,

Rui Barradas
Em 30-10-2012 08:16, Mark Payne escreveu:
Hi,

I very frequently end up in a situation where I have a named list of
data.frames that I wish to combine. e.g.

l <- list(A=data.frame(x=rnorm(5),
y=rnorm(5)),
           B=data.frame(x=rnorm(3),y=rnorm(3)),
           C=data.frame(x=rnorm(4),y=rnorm(4)),
           D=data.frame(x=rnorm(7),y=rnorm(7)))

I would like to combine these data.frames into a single data.frame,
with the column-names. This is easy with rbind and do.call

l2 <- do.call(rbind,l)

However, I would also like l2 to contain a column containing the names
in the original list? Is there a more elegant way to do this than:

l2 <- do.call(rbind,l)
l2$name <- rep(names(l),times=sapply(l,nrow))


Mark

______________________________________________
R-help@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.

______________________________________________
R-help@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.

Reply via email to