[R] Removing a space from a string
Rasmus Liland
jr@| @end|ng |rom po@teo@no
Tue Jul 28 23:00:51 CEST 2020
Dear Dennis,
On 2020-07-28 13:20 -0700, Dennis Fisher wrote:
| Colleagues
|
| I have strings that contain a space in
| an unexpected location. The intended
| string is:
| “STRING 01. Remainder of the string"
| However, variants are:
| “STR ING 01. Remainder of the string"
| “STRIN G 01. Remainder of the string"
|
| I would like a general approach to
| deleting a space, but only if it
| appears before the period. Any
| suggestions on a regular expression
| for this?
Perhaps by using gregexpr to look for
dots, remove spaces from the substring until the first
finding, then pasting it back.
strings <-
c("STRING 01. Remainder of the string.",
"STR ING 01. Remainder of the string.",
"STRIN G 01. Remainder of the string.")
search <- gregexpr("\\.", strings)
lens <- nchar(strings)
FUN <- function(i, strings, search, lens) {
before.dot <- substr(strings[i], 1, search[[i]][1])
before.dot <- gsub(" ", "", before.dot)
after.dot <- substr(strings[i], search[[i]][1]+1, lens[i])
return(paste0(before.dot, after.dot))
}
simplify2array(parallel::mclapply(
X=1:length(strings),
FUN=FUN,
strings=strings,
search=search,
lens=lens))
yields
[1] "STRING01. Remainder of the string."
[2] "STRING01. Remainder of the string."
[3] "STRING01. Remainder of the string."
Yes, I know, the space just before 01
also disappears ...
Best,
Rasmus
More information about the R-help
mailing list