Home > OS >  replace the word in which there is a symbol
replace the word in which there is a symbol

Time:09-23

I have the data

vec <- c("RT_12_ SDB", "ZT_12_", "_12TR", "RE_12_TR")

I want to replace all the data that has "_12" on "12"

what I want to get:

vec <- c(12 SDB,12,12,12)

I tried

vec <- gsub("\\b_12_\\w ", "12", c("RT_12_", "ZT_12_", "_12TR", "RE_12_TR"))

but it didn't work

CodePudding user response:

Why not just extract 12 from the data ?

as.numeric(stringr::str_extract(vec, '12'))
#[1] 12 12 12 12

In base R,

as.numeric(sub('.*(12).*', '\\1', vec))

For the updated data we can use -

sub("(?<=12)[A-Z] ", "", gsub('.*(?=12)|_', '', vec, perl = TRUE),perl = TRUE)
#[1] "12 SDB" "12"     "12"     "12" 

where the inner gsub removes everything until 12 and the underscore whereas the outer sub drops the characters after 12.

CodePudding user response:

You could try this

gsub('.*(12(_\\sSDB)?).*', '\\1', vec)
# [1] "12_ SDB" "12"      "12"      "12"    
  • Related