Creating duplicate row(s)

Hello All,

I have a scenario I am facing.

I need to duplicate (replicate) certain rows in a DF I am working with.

Due to the sensitivity of the data I am working with, I provided a mock of what could be done with the output below.

employee <- c('John Doe','Peter Gynn','Jolie Hope')
salary <- c(21000, 23400, 26800)
startdate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14'))

employ.data <- data.frame(employee, salary, startdate)

employee salary startdate
John Doe 21000 2010-11-01
Peter Gynn 23400 2008-03-25
Jolie Hope 26800 2007-03-14

My question is, lets say I want to duplicate the row of data pertaining to Peter Gynn in the output above.

So I would want two similar rows of data over Peter Gynn

How would I do that? I cannot seem to find the proper syntax to figure out this? I may also need to do it by a condition where if a row contains X or in this example, Peter Gynn in a column, make a duplicate row.

All the help is greatly appreciated.

Thank you so much!

Would something like this work?

rbind(employ.data, employ.data[employee == "Peter Gynn", ])

It did, but it put that new duplicate at the bottom of the DF.

Would you happen to know how to round out syntax and have that newly duplicated row show below the original row?

Appreciate your help so much!

Thank you!

employ.data[c(1, 2, 2, 3), ]

Arthur,

Thank you so much!

Would you happen to also know how to use that with dplyr and use the pipe operator?

All the help is greatly appreciated, thank you!

employ.data %>% .[c(1, 2, 2, 3), ]
or
employ.data %>% slice(c(1, 2, 2, 3))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.