velvetrock
velvetrock

Reputation: 585

Remove numbers from string in R

I'm trying to remove all the number except 67 from string by using the function gsub.

For example:

txt <- "A function 147832 for 67cleaning 67 data 6 7"

Desire output:

txt <- "A function for 67cleaning 67 data"

I've tried txt = gsub("[[:digit:]]", "", txt), but it will remove all the numbers.

Upvotes: 5

Views: 15079

Answers (2)

stas g
stas g

Reputation: 1513

it's not super elegant but you can do it in three steps:

 tmp <- gsub("67", "XX", "A function 147832 for 67cleaning 67 data 6 7")
 tmp <- gsub("\\d+", "", tmp)
 tmp <- gsub("XX", "67", tmp)
 tmp
 #"A function  for 67cleaning 67 data  "

first substitute all instances of 67 with a marker (say, XX), then delete all other remaining numbers, finally sub 67 back in.

Upvotes: 4

Veerendra Gadekar
Veerendra Gadekar

Reputation: 4472

You could do this

x = unlist(strsplit(txt, split = '\\s+')) # split your string
paste0(x[Reduce(`|`, lapply(c('[A-Za-z]', '67'), grepl, x))], collapse = ' ') # use the list of regular expression to match the required pattern and put them all together

#[1] "A function for 67cleaning 67 data"

Upvotes: 2

Related Questions