Replace character at certain position within string for each row in a data frame

22 Views Asked by At

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) 
0

There are 0 best solutions below