How to repeat a whole data frame in R?

R ProgrammingServer Side ProgrammingProgramming

To repeat a whole data frame in R, we can follow the below steps −

  • First of all, create a data frame.

  • Then, use rep function to repeat the data frame.

Example

Create the data frame

Let’s create a data frame as shown below −

x<-rnorm(20)
y<-rnorm(20)
z<-rnorm(20)
df<-data.frame(x,y,z)
df

Output

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

         x           y          z
1  -0.72553554 -0.003781742 -1.36481696
2  -0.12039265 -0.208840085  0.81715654
3  -1.97592285  0.887936261  1.98675032
4   0.17376084  0.515153192  0.69978704
5  -1.07646565  0.167017490 -2.30298717
6   0.98623624  0.481496490  0.55212882
7   1.66696821  1.454184146 -1.01862076
8   1.00844431 -0.722831649 -0.23451775
9   0.53058163  0.485509531  1.45703838
10  0.23904645  1.456821988 -0.11157839
11  0.50032955 -1.276985702  0.24204625
12  2.28643294 -0.189765951 -0.16021579
13 -0.37134394 -1.144893466 -1.84426120
14 -0.84774565  0.304323165  0.99355776
15  0.03875027  0.034829337  1.19431531
16 -2.13755826  0.877838682 -0.08493668
17  0.66341839  0.910711816  1.75597566
18 -0.76871288  0.998276851 -0.66642423
19  1.85887892  0.762980833  0.23355600
20  0.65030663  2.218971071 -0.35667725

Repeat the data frame

Using rep function to repeat the data frame df two times −

x<-rnorm(20)
y<-rnorm(20)
z<-rnorm(20)
df<-data.frame(x,y,z)
df[rep(1:nrow(df),2),]

Output

           x           y           z
1    -0.72553554 -0.003781742 -1.36481696
2    -0.12039265 -0.208840085  0.81715654
3    -1.97592285  0.887936261  1.98675032
4     0.17376084  0.515153192  0.69978704
5    -1.07646565  0.167017490 -2.30298717
6     0.98623624  0.481496490  0.55212882
7     1.66696821  1.454184146 -1.01862076
8     1.00844431 -0.722831649 -0.23451775
9     0.53058163  0.485509531  1.45703838
10    0.23904645  1.456821988 -0.11157839
11    0.50032955 -1.276985702  0.24204625
12    2.28643294 -0.189765951 -0.16021579
13   -0.37134394 -1.144893466 -1.84426120
14   -0.84774565  0.304323165  0.99355776
15    0.03875027  0.034829337  1.19431531
16   -2.13755826  0.877838682 -0.08493668
17    0.66341839  0.910711816  1.75597566
18   -0.76871288  0.998276851 -0.66642423
19    1.85887892  0.762980833  0.23355600
20    0.65030663  2.218971071 -0.35667725
1.1  -0.72553554 -0.003781742 -1.36481696
2.1  -0.12039265 -0.208840085  0.81715654
3.1  -1.97592285  0.887936261  1.98675032
4.1   0.17376084  0.515153192  0.69978704
5.1  -1.07646565  0.167017490 -2.30298717
6.1   0.98623624  0.481496490  0.55212882
7.1   1.66696821  1.454184146 -1.01862076
8.1   1.00844431 -0.722831649 -0.23451775
9.1   0.53058163  0.485509531  1.45703838
10.1  0.23904645  1.456821988 -0.11157839
11.1  0.50032955 -1.276985702  0.24204625
12.1  2.28643294 -0.189765951 -0.16021579
13.1 -0.37134394 -1.144893466 -1.84426120
14.1 -0.84774565  0.304323165  0.99355776
15.1  0.03875027  0.034829337  1.19431531
16.1 -2.13755826  0.877838682 -0.08493668
17.1  0.66341839  0.910711816  1.75597566
18.1 -0.76871288  0.998276851 -0.66642423
19.1  1.85887892  0.762980833  0.23355600
20.1  0.65030663  2.218971071 -0.35667725
raja
Published on 15-Nov-2021 10:30:32
Advertisements