duplicate every row in a table and add a suffix in r

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.r
# load the iris dataset
data(iris)

# duplicate every row and add "_copy" suffix
iris_copy <- rbind(iris, iris)
iris_copy$ID <- rep(1:nrow(iris), 2)
iris_copy$ID <- paste0(iris_copy$ID, "_copy")

# view the first few rows of the copied dataset
head(iris_copy)
260 chars
11 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