1

I am basically trying to produce a REALLY large dataset for testing purposes, and I have an example dataset with 600,000 rows.

I want to append this df to itself n times, how would I do this?

I could cbind it to itself multiple times manually, but there must be an automated way to do specify the number of times I want it to append to itself?

3 Answers3

2

You could use [ and repeat the relevant row indices. Example:

df <- data.frame(x = 1:2, y = letters[11:12])
df[rep(1:nrow(df), 10), ] # or rep(seq_len(nrow(df)), 10)

    x y
1   1 k
2   2 l
1.1 1 k
2.1 2 l
1.2 1 k
2.2 2 l
1.3 1 k
2.3 2 l
1.4 1 k
2.4 2 l
1.5 1 k
2.5 2 l
1.6 1 k
2.6 2 l
1.7 1 k
2.7 2 l
1.8 1 k
2.8 2 l
1.9 1 k
2.9 2 l
s_baldur
  • 29,441
  • 4
  • 36
  • 69
0

Consider replicate (wrapper to sapply) to build a list of duplicate data frames to then run cbind (or rbind for append) once at end:

n <- 5
df_list <- replicate(n, mydata, simplify = FALSE)

final_df <- do.call(cbind.data.frame, df_list)
head(final_df)

Online demo

Parfait
  • 104,375
  • 17
  • 94
  • 125
0

Here is another solution using dplyr

library(dplyr)

#bind by column the same df repeated n times (in this example = 3)
rep_df<-bind_cols(rep(df,3))