Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (5.3k points)
edited by

I have the following data frame

data.frame(a = c(1,2,3),b = c(1,2,3))

  a b

1 1 1

2 2 2

3 3 3

and I want to turn it into

  a b

1 1 1

2 2 2

3 3 3

4 1 1

5 2 2

6 3 3

7 1 1

8 2 2

9 3 3

or repeat it N times. Is there an easy function to do this in R? Thanks!

1 Answer

0 votes
by
edited by

To repeat a data frame N times, you can use the replicate function along with the rbind function  as follows:

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

n <- 3

do.call("rbind", replicate(n, d, simplify = FALSE))

  a b

1 1 1

2 2 2

3 3 3

4 1 1

5 2 2

6 3 3

7 1 1

8 2 2

9 3 3

If you want to learn more about R programming watch this tutorial on Introduction to Data Science with R

Browse Categories

...