When df$v1and df$v2are symbols, we can use
regmatches(df$v1, gregexpr("\\+", df$v1)) <- regmatches(df$v2, gregexpr("\\+", df$v1))
I.e,
df <- data.frame(v1 = c("+AA+B", "D++CC", "A+BAD"),
v2 = c("DBBAD", "BDCAD", "CDCCA"),
stringsAsFactors = FALSE)
rg <- gregexpr("\\+", df$v1)
regmatches(df$v1, rg) <- regmatches(df$v2, rg)
df
rgcontains the positions "+" in df$v1, and we conveniently use it regmatchesto replace those matches with df$v1what is in df$v2at the same positions.
source
share