Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

duplicate rows in a data frame in R

Tags:

dataframe

r

I am trying to duplicate rows in my data frame using the code below. However, I'm finding it to be slow.

duprow = df[1,]
for(i in 1:2000)
{
    print(i)
    df = rbind(df,duprow)
}

Is there a faster way?

like image 853
tubby Avatar asked Apr 20 '15 09:04

tubby


People also ask

How do I duplicate a row in a DataFrame in R?

The way to repeat rows in R is by using the REP() function. The REP() function is a generic function that replicates the value of x one or more times and it has two mandatory arguments: x: A vector.

Can DataFrame have duplicate rows?

DataFrame. duplicated() method is used to find duplicate rows in a DataFrame. It returns a boolean series which identifies whether a row is duplicate or unique.

Can you have duplicate row names in R?

It is not possible to have duplicate row names, but a simple workaround is creating an extra column (e.g. label) that holds the name that you would assign to your rows.


1 Answers

You can use rep, e.g. for 5 duplicates or row 1:

df <- data.frame(x = 1, y = 1)
rbind(df, df[rep(1, 5), ])
#     x y
# 1   1 1
# 11  1 1
# 1.1 1 1
# 1.2 1 1
# 1.3 1 1
# 1.4 1 1
like image 92
lukeA Avatar answered Oct 01 '22 08:10

lukeA