Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove periods before the first comma in a string

Tags:

regex

r

gsub

How can I remove the periods before the first comma in these strings?

 xx <- c("fefe.3. fregg, ff, 34.gr. trgw", 
          "fefe3. fregg, ff, 34.gr. trgw",
          "fefe3 fregg, ff, 34.gr. tr.gw")

Desired output:

    "fefe3 fregg, ff, 34.gr. trgw"
    "fefe3 fregg, ff, 34.gr. trgw"
    "fefe3 fregg, ff, 34.gr. tr.gw" 

I've started with gsub("\\.","", xx)), which removes all periods. How to change it to specify 'only the period before the first comma'?

like image 233
Ben Avatar asked Jan 13 '23 00:01

Ben


1 Answers

I feel like this is cheating, but it works for this simple example....

xx <- c("fefe.3. fregg, ff, 34.gr. trgw", 
        "fefe3. fregg, ff, 34.gr. trgw",
        "fefe3 fregg, ff, 34.gr. tr.gw")

temp <- strsplit(xx, ",")

sapply(seq_along(temp), function(x) {
  t1 <- gsub("\\.", "", temp[[x]][1])
  paste(t1, temp[[x]][2], temp[[x]][-c(1, 2)], sep = ",")
})
# [1] "fefe3 fregg, ff, 34.gr. trgw"  "fefe3 fregg, ff, 34.gr. trgw" 
# [3] "fefe3 fregg, ff, 34.gr. tr.gw"

The basic idea above is that since you're only going to be looking for a period in the first chunk before a comma, why not split it and use a basic gsub on that, and then put the pieces back together. Unlikely to be efficient....

like image 103
A5C1D2H2I1M1N2O1R2T1 Avatar answered Jan 18 '23 23:01

A5C1D2H2I1M1N2O1R2T1