## Repeat rows of a data.frame

**This question already has answers here**:

df <- data.frame(a = 1:2, b = letters[1:2]) df[rep(seq_len(nrow(df)), each = 2), ]

**Repeat rows of a data.frame N times,** EDIT: updated to a better modern R answer. You can use replicate() , then rbind the result back together. The rownames are automatically altered to run from Repeat or replicate the rows of dataframe in pandas python (create duplicate rows) can be done in a roundabout way by using concat () function. Repeat or replicate the dataframe in pandas python. Repeat or replicate the dataframe in pandas along with index.

A clean `dplyr`

solution, taken from here

library(dplyr) df <- tibble(x = 1:2, y = c("a", "b")) df %>% slice(rep(1:n(), each = 2))

**Repeat rows of a data.frame,** Repeat rows of a data. frame. I want to repeat the rows of a data. Example for N = 2: So, each row is repeated 2 times and characters remain characters, factors remain factors, numerics remain numerics, My first attempt used apply: apply(old.df, 2, function(co) rep(co, each = N)), but this one transforms my values To repeat rows of a data frame n times, you use the following methods: df <- data.frame(A=c("j","K"), B=c("i","P"),C = c(100,101)) df[rep(seq_len(nrow(df)), each=2),] df[rep(seq_len(nrow(df)), each=2),] A B C. 1 j i 100. 1.1 j i 100. 2 K P 101. 2.1 K P 101. Using dplyr: df %>% slice(rep(1:n(), each = 2)) A B C. 1 j i 100. 2 j i 100. 3 K P 101. 4 K P 101

If you can repeat the whole thing, or subset it first then repeat that, then this similar question may be helpful. Once again:

library(mefa) rep(mtcars,10)

or simply

mefa:::rep.data.frame(mtcars)

**rep.data.frame function,** Each rows of the data frame x will be repeated as many times as the corresponding value in e.g. the vector times . This function is called internally by the function stcs . d <- data.frame(a = c(1,2,3),b = c(1,2,3)) n <- 3 do.call("rbind", replicate(n, d, simplify = FALSE)) A more traditional way is to use indexing, but here the rowname altering is not quite so neat (but more informative):

There is a lovely vectorized solution that repeats only certain rows n-times each, possible for example by adding an `ntimes`

column to your data frame:

A B C ntimes 1 j i 100 2 2 K P 101 4 3 Z Z 102 1

Method:

df <- data.frame(A=c("j","K","Z"), B=c("i","P","Z"), C=c(100,101,102), ntimes=c(2,4,1)) df <- as.data.frame(lapply(df, rep, df$ntimes))

Result:

A B C ntimes 1 Z Z 102 1 2 j i 100 2 3 j i 100 2 4 K P 101 4 5 K P 101 4 6 K P 101 4 7 K P 101 4

This is very similar to Josh O'Brien and Mark Miller's method:

df[rep(seq_len(nrow(df)), df$ntimes),]

However, that method appears quite a bit slower:

df <- data.frame(A=c("j","K","Z"), B=c("i","P","Z"), C=c(100,101,102), ntimes=c(2000,3000,4000)) microbenchmark::microbenchmark( df[rep(seq_len(nrow(df)), df$ntimes),], as.data.frame(lapply(df, rep, df$ntimes)), times = 10 )

Result:

Unit: microseconds expr min lq mean median uq max neval df[rep(seq_len(nrow(df)), df$ntimes), ] 3563.113 3586.873 3683.7790 3613.702 3657.063 4326.757 10 as.data.frame(lapply(df, rep, df$ntimes)) 625.552 654.638 676.4067 668.094 681.929 799.893 10

**A quick way to do row repeat and col repeat (rep.row ,** Click here if you're looking to post or find an R/data-science job. Want to share your content on R-bloggers? click here if you have a blog, or here I want to repeat the rows of a data.frame, each N times. The result should be a new data.frame (with nrow (new.df) == nrow (old.df) * N) keeping the data types of the columns. So, each row is repeated 2 times and characters remain characters, factors remain factors, numerics remain numerics,

The rep.row function seems to sometimes make lists for columns, which leads to bad memory hijinks. I have written the following which seems to work well:

library(plyr) rep.row <- function(r, n){ colwise(function(x) rep(x, n))(r) }

**Repeat or replicate the rows of dataframe in pandas python (create ,** Repeat or replicate the rows of dataframe in pandas python (create duplicate rows) can be done in a roundabout way by using concat() function. example of .. In this example, we will create a dataframe with a duplicate row of another. We shall use unique function to remove these duplicate rows. > DF1 = data . frame ( C1 = c ( 1 , 5 , 14 , 1 , 54 ), C2 = c ( 9 , 15 , 85 , 9 , 42 ), C3 = c ( 8 , 7 , 42 , 8 , 16 ))

**Identify and Remove Duplicate Data in R,** Extract unique elements; Remove duplicate rows in a data frame; Summary. Required packages. Load the tidyverse packages, which include dplyr Each rows of the data frame x will be repeated as many times as the corresponding value in e.g. the vector times. This function is called internally by the function stcs.

**[R] repeating a dataframe n times in the direction of the rows,** [R] repeating a dataframe n times in the direction of the rows. Peter Dalgaard BSA p.dalgaard at biostat.ku.dk. Mon Nov 11 12:30:38 CET 2002. Previous This is my data frame that should be repeated for 5 times: I wanna have the result like this: But there must be a way smarter than keep appending.. Actually the data frame Im working on should be repeated for 50 times..

**Drop Duplicate Rows in a DataFrame,** This post shows how to remove duplicate records and combinations of columns in a Pandas dataframe and keep only the unique values. Imports. import pandas Today I worked on a simulation program which require me to create a matrix by repeating the vector n times (both by row and by col). Even the task is extremely simple and only take 1 line to finish(10sec), I have to think about should the argument in rep be each or times and should the argument in matrix is nrow or ncol.

##### Comments

- You can use
`n.times <- c(2,4) ; df[rep(seq_len(nrow(df)), n.times),]`

if you want to vary the number of times each line is repeated. - Aha! Another brilliant R function hidden deep inside an obcure specialist package with a totally unrelated name. I love this language!