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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With