Chris Chris - 5 months ago 12
R Question

How to assign a value for a column based on based on another column value in R?

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!


May be this helps

df$col4 <- paste0("newValue_", cumsum(!duplicated(df$col2)))
#[1] "newValue_1" "newValue_1" "newValue_1" "newValue_1" "newValue_2"

Or we use match

with(df, paste0("newValue_", match(col2, unique(col2))))
#[1] "newValue_1" "newValue_1" "newValue_1" "newValue_1" "newValue_2"

Or it can be done with factor

with(df, paste0("newValue_", as.integer(factor(col2, levels = unique(col2)))))