In R, for a general data frame, I want to replace the character at position with replacement for each row.
# I want to replace the character at position with replacement for each row
mat = data.frame(
text = c('Hello', 'Hi', 'Welcome'),
position = c(3, 2, 1),
replacement = c('X', 'X', 'X'),
stringsAsFactors = F
)
# I can do the replacement for a single string like this:
substr(mat$text[1],3,3) <-'X'
#But if I apply it to the whole data.frame it doesn't work:
mat %>%
mutate(text.replace = substr(text, position, position) <- replacement)