I have a dataframe
df <- data.frame(structure(list(col1= c("A", "B", "C", "D", "A"),
col2= c(1, 1, 1, 1, 5), col3 = c(2L, 1L, 1L, 1L, 1L)),
.Names = c("col1", "col2", "col3"),
row.names = c(NA, -5L), class = "data.frame"))
I want to add additional column, col4 with values based on col2. Rows that have the same value in col2 will have the same value in col4 as well.
With a work around, I generated a result in the following way.
x <- df[!duplicated(df$col2),]
x$col4 <- paste("newValue", seq(1:nrow(x)), sep="_")
df_new <- merge(x, df, by ="col2")
df_new <- df_new[,c("col2","col4", "col1.y", "col3.y")]
This works but I thought there is a better way doing this. Thank you!