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'?
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....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With