Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding a list of vectors to a data.frame in R

Tags:

r

How can I add a list of vectors to a preallocated data.frame such that the vectors form the rows of the data.frame?

eg.

ll<-list(c(1,2,3),c(2,3,4))
dd<-data.frame(matrix(nrow=10,ncol=3))  

I need dd to look like

1 2 3
2 3 4
like image 660
aks Avatar asked Nov 22 '25 09:11

aks


1 Answers

do.call(rbind, ll)

To add to an existing data frame

rbind(dd, do.call(rbind, ll)
# OR 
do.call(rbind, c(ll, list(dd))

If you're doing this for many data frames, see rbind.fill in the plyr package for a much more efficient approach.

like image 88
hadley Avatar answered Nov 24 '25 23:11

hadley



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!