How to replicate whole data frame and add it in the original one in R?


The replicates of a data frame in R can be created with the help of sapply function, to set the number of times we want to repeat the data frame we can use rep.int,times argument. For example, if we have a data frame df and we want to create 5 replicates of df and add them in the original then sapply(df,rep.int,times=5) can be used.

Example

 Live Demo

Consider the below data frame −

set.seed(151)
x1<-rnorm(5,21,3)
x2<-rnorm(5,8,1.9)
x3<-rnorm(5,3,0.08)
x4<-rnorm(5,1008,32.4)
df<-data.frame(x1,x2,x3,x4)
df

Output

      x1       x2       x3       x4
1 20.84538 9.486324 2.961236 967.9296
2 23.29721 5.344792 3.044849 960.2204
3 20.55978 6.064207 3.005293 1086.9639
4 20.66044 8.436004 2.892010 1029.8222
5 19.81347 9.277129 2.980567 1018.0453

Replicating df two times −

sapply(df,rep.int,times=2)

Output

      x1       x2       x3       x4
[1,] 20.84538 9.486324 2.961236 967.9296
[2,] 23.29721 5.344792 3.044849 960.2204
[3,] 20.55978 6.064207 3.005293 1086.9639
[4,] 20.66044 8.436004 2.892010 1029.8222
[5,] 19.81347 9.277129 2.980567 1018.0453
[6,] 20.84538 9.486324 2.961236 967.9296
[7,] 23.29721 5.344792 3.044849 960.2204
[8,] 20.55978 6.064207 3.005293 1086.9639
[9,] 20.66044 8.436004 2.892010 1029.8222
[10,] 19.81347 9.277129 2.980567 1018.0453

Replicating df three times −

sapply(df,rep.int,times=3)

Output

      x1       x2       x3       x4
[1,] 20.84538 9.486324 2.961236 967.9296
[2,] 23.29721 5.344792 3.044849 960.2204
[3,] 20.55978 6.064207 3.005293 1086.9639
[4,] 20.66044 8.436004 2.892010 1029.8222
[5,] 19.81347 9.277129 2.980567 1018.0453
[6,] 20.84538 9.486324 2.961236 967.9296
[7,] 23.29721 5.344792 3.044849 960.2204
[8,] 20.55978 6.064207 3.005293 1086.9639
[9,] 20.66044 8.436004 2.892010 1029.8222
[10,] 19.81347 9.277129 2.980567 1018.0453
[11,] 20.84538 9.486324 2.961236 967.9296
[12,] 23.29721 5.344792 3.044849 960.2204
[13,] 20.55978 6.064207 3.005293 1086.9639
[14,] 20.66044 8.436004 2.892010 1029.8222
[15,] 19.81347 9.277129 2.980567 1018.0453

Replicating df four times −

Example

sapply(df,rep.int,times=4)

Output

      x1       x2       x3       x4
[1,] 20.84538 9.486324 2.961236 967.9296
[2,] 23.29721 5.344792 3.044849 960.2204
[3,] 20.55978 6.064207 3.005293 1086.9639
[4,] 20.66044 8.436004 2.892010 1029.8222
[5,] 19.81347 9.277129 2.980567 1018.0453
[6,] 20.84538 9.486324 2.961236 967.9296
[7,] 23.29721 5.344792 3.044849 960.2204
[8,] 20.55978 6.064207 3.005293 1086.9639
[9,] 20.66044 8.436004 2.892010 1029.8222
[10,] 19.81347 9.277129 2.980567 1018.0453
[11,] 20.84538 9.486324 2.961236 967.9296
[12,] 23.29721 5.344792 3.044849 960.2204
[13,] 20.55978 6.064207 3.005293 1086.9639
[14,] 20.66044 8.436004 2.892010 1029.8222
[15,] 19.81347 9.277129 2.980567 1018.0453
[16,] 20.84538 9.486324 2.961236 967.9296
[17,] 23.29721 5.344792 3.044849 960.2204
[18,] 20.55978 6.064207 3.005293 1086.9639
[19,] 20.66044 8.436004 2.892010 1029.8222
[20,] 19.81347 9.277129 2.980567 1018.0453

Replicating df five times −

Example

sapply(df,rep.int,times=5)

Output

      x1          x2       x3    x4
[1,] 20.84538 9.486324 2.961236 967.9296
[2,] 23.29721 5.344792 3.044849 960.2204
[3,] 20.55978 6.064207 3.005293 1086.9639
[4,] 20.66044 8.436004 2.892010 1029.8222
[5,] 19.81347 9.277129 2.980567 1018.0453
[6,] 20.84538 9.486324 2.961236 967.9296
[7,] 23.29721 5.344792 3.044849 960.2204
[8,] 20.55978 6.064207 3.005293 1086.9639
[9,] 20.66044 8.436004 2.892010 1029.8222
[10,] 19.81347 9.277129 2.980567 1018.0453
[11,] 20.84538 9.486324 2.961236 967.9296
[12,] 23.29721 5.344792 3.044849 960.2204
[13,] 20.55978 6.064207 3.005293 1086.9639
[14,] 20.66044 8.436004 2.892010 1029.8222
[15,] 19.81347 9.277129 2.980567 1018.0453
[16,] 20.84538 9.486324 2.961236 967.9296
[17,] 23.29721 5.344792 3.044849 960.2204
[18,] 20.55978 6.064207 3.005293 1086.9639
[19,] 20.66044 8.436004 2.892010 1029.8222
[20,] 19.81347 9.277129 2.980567 1018.0453
[21,] 20.84538 9.486324 2.961236 967.9296
[22,] 23.29721 5.344792 3.044849 960.2204
[23,] 20.55978 6.064207 3.005293 1086.9639
[24,] 20.66044 8.436004 2.892010 1029.8222
[25,] 19.81347 9.277129 2.980567 1018.0453

Updated on: 17-Oct-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements