[R] Removing a space from a string
Rasmus Liland
jr@| @end|ng |rom po@teo@no
Tue Jul 28 23:25:14 CEST 2020
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20200728/59f0f2f3/attachment.sig>
More information about the R-help
mailing list