I have a data like this
clas=c("CD_1","X.2_2","K$2_3","12k3_4",".A_5","xy_6")
df <- data.frame(clas)
> df
clas
1 CD_1
2 X.2_2
3 K$2_3
4 12k3_4
5 .A_5
6 xy_6
and I would like to change some rows that match this condition
if the strings after _
are 4,5 and 6 replace the strings before the _
with string B
. So the output should like this;
clas
1 CD_1
2 X.2_2
3 K$2_3
4 12kB_4
5 .B_5
6 xB_6
Thanks!
EDIT::
SO If I have data like this:
clas
1 CD_1
2 X.2_2
3 K$2_3
4 12k3_4
5 .A_5
6 xy_11
Then applying your solution,
df %>% mutate(clas = str_replace(clas, "(.)(_[4511])", "B\\2"))
clas
1 CB_1
2 X.2_2
3 K$2_3
4 12kB_4
5 .B_5
6 xB_11
But I only want to match 11
not 1
. How can we do that ?