0

I have a data frame that I want to repeat. Ie I want to do this df<-bind_rows(df,df,df) But ideally df would be repeated 10000 times. I have not found an optimal way to do this. Any ideas.

MC101
  • 87
  • 5

2 Answers2

2

Simplify=False was what I was missing. df<-bind_rows(replicate(N,df, simplify=False)) Seems to be the best answer.

MC101
  • 87
  • 5
  • 1
    Ok, you can upvote Stibu's answer in the linked question, which is more comprehensive and correctly writes FALSE. – Frank Aug 11 '17 at 15:40
1

Start with your data.frame:

df <- data.frame(a=c(1, 2), b=c(3, 4))

Then, you can use a for loop:

out <- df
for (i in seq_len(9999)) {
    out <- rbind(out, df)
}

Then, out will be df repeated 10000 times. The only downside is that it will take approximately until the heat death of the universe. Or, a better, faster, way:

do.call("rbind", replicate(10000, df, simplify = FALSE))

Will duplicate df 10000 times, and then rbind all the results together.

Julian Zucker
  • 564
  • 4
  • 13
  • 1
    I think you ought to clarify that the loop way is really, really bad. If unsure what I mean, see "Circle 2" of the R Inferno http://www.burns-stat.com/documents/books/the-r-inferno/ – Frank Aug 11 '17 at 15:30
  • 1
    Good call. Added some wit about the heat death of the universe, I hope that helps dissuade people from growing their data. That book is great, and should be automatically appended (or prepended) to every answer on questions tagged with R. – Julian Zucker Aug 11 '17 at 16:46