Let's say I have below vector
Vec = c("AA", "BB1", "CC2")
Now I want to replace few specific elements based on pattern match as follows
Vec[grepl("[0-9]+$", Vec)] <- paste("CCC", grep("[0-9]+$", Vec, value = T))
Vec
## [1] "AA" "CCC BB1" "CCC CC2"
How can I execute above step using pipe operator? Below is my try
library(dplyr)
Vec = c("AA", "BB1", "CC2")
Vec %>% `<-`(.[grepl("[0-9]+$", .)], paste("CCC", grep("[0-9]+$", ., value = T)))
With that, I am getting below error
Error in Vec %>% (.[grepl("[0-9]+$", .)] <- paste("CCC", grep("[0-9]+$", :
incorrect number of arguments to "<-"
What is the right approach?