To duplicate every row in a table and add a suffix in R, you can use the rbind()
function to combine the original table with a modified version where the suffix is added to each row.
Here's an example using the iris
dataset:
main.r260 chars11 lines
In this code, we first load the iris
dataset. Then, we use rbind()
to concatenate the original iris
dataframe with itself. We add a new column named "ID" that contains a suffix of "_copy" to identify the duplicated rows. We then use the paste0()
function to concatenate the row number with the suffix, and finally assign the modified dataframe to a new variable named iris_copy
. The head()
function is used to display the first few rows of the copied dataset.
gistlibby LogSnag