when patterns of numbers match in two columns replace the second column with the value in the first in r

# Sample data
df <- data.frame(col1 = c("123", "456", "789"),
                 col2 = c("123", "555", "789"))

# Replacing values in col2 with col1 where they match
df$col2 <- ifelse(df$col1 == df$col2, df$col1, df$col2)
df
224 chars
8 lines

related categories

gistlibby LogSnag