On 2020-07-28 23:00 +0200, Rasmus Liland wrote:
|
| 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 ...
I forgot about regexpr ... this is
simpler I think:
strings <-
c("STRING 01. Remainder of the string.",
"STR ING 01. Remainder of the string.",
"STRIN G 01. Remainder of the string.")
search <- regexpr("...\\.", strings) # search for the first dot and
three chars in front of it
ml <- attr(search, "match.length")
paste0(
gsub(" ", "", substr(strings, 1, search)),
substr(strings, search, search+ml-1),
substr(strings, search+ml, nchar(strings))
)
/Rasmus
signature.asc
Description: PGP signature
______________________________________________ [email protected] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.

