I would like to replace a part of a string (between the first 2 underscores, the first group always being "i") like in the base R example below:
library(dplyr)
library(stringr)
d <- tibble(txt = c("i_0000_GES", "i_0000_OISO", "i_0000_ASE1333"),
repl = c("1111", "1111", "2222"))
str_sub(d$txt, 3, 6) <- d$repl
d
# A tibble: 3 x 2
# txt repl
# <chr> <chr>
# 1 i_1111_GES 1111
# 2 i_1111_OISO 1111
# 3 i_2222_ASE1333 2222
How can I do that either using str_sub<-
or another stringr-function?