[R] Removing a space from a string
bgunter@4567 @end|ng |rom gm@||@com
Tue Jul 28 23:53:31 CEST 2020
1. Thanks for the nice reprex.
2. However, I thought there was still a bit of ambiguity. I interpreted
your specification to mean: "any number of spaces could occur in the
beginning alphabetic part of the strings before one or more digits occur
followed by a '.' (a period) and then more stuff after."
3. My strategy was simply to split the strings into the first part
consisting of the alphabetic characters and spaces and the second part with
the numbers and everything else. Then I just removed the spaces in the
first part. You can then concatenate them together again (using paste())
however you like. Thus
 "STRING 01. Remainder of the string" "STR ING 01. Remainder of the
 "STRIN G 01. Remainder of the string"
> p1 <-gsub(" ","",gsub("([^[:digit:]]+)[[:digit:]]+\\..*$","\\1",x))
> p2 <- gsub("[^[:digit:]]+([[:digit:]]+\\..*$)","\\1",x)
 "STRING" "STRING" "STRING"
 "01. Remainder of the string" "01. Remainder of the string"
 "01. Remainder of the string"
I look forward to better approaches using basic regex's (no additional
"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Tue, Jul 28, 2020 at 1:20 PM Dennis Fisher <fisher using plessthan.com> wrote:
> R 4.0.2
> OS X
> 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
> Dennis Fisher MD
> P < (The "P Less Than" Company)
> Phone / Fax: 1-866-PLessThan (1-866-753-7784)
> www.PLessThan.com <http://www.plessthan.com/>
> [[alternative HTML version deleted]]
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
More information about the R-help