Re: [R] Removing a space from a string

2020-07-29 Thread Dennis Fisher
Richard Per your requests: 1. Plain text: no spaces 2. read_docx: spaces 3. read_rtf: no spaces 4. Not requested by you: copying from the Word document, then pasting into “vim”: no spaces The Word document was created by hand but #1, #3, and #4 confirm that i

Re: [R] Removing a space from a string

2020-07-29 Thread Kimmo Elo
Hi! How about this? --- snip --- > x <- c("STRING 01. Remainder of the string","STR ING 01. Remainder of the string","STRIN G 01. Remainder of the string","STR IN G 01. Remainder of the string") > x1<-unlist(strsplit(x,"\\.")) > for (i in seq(1,length(x1),2)) { x[(i+1) %/% 2]<-paste(gsub(

Re: [R] Removing a space from a string

2020-07-28 Thread Richard O'Keefe
The spaces may not have been VISIBLE in the Word document, but that does not mean that there wasn't anything THERE. - What happens if you open the document in Word and save it as plain text? - What happens if you open the document in Word and save it as RTF, then read that using read_rtf? - If

Re: [R] Removing a space from a string

2020-07-28 Thread Dennis Fisher
Richard In reply to your “first response”, the text was originally in a Word document and it did NOT contain the errant spaces. I used read_docx in the textreadr package to access the text. The spaces were added during that step. I am copying the maintainer of that package to see if he has a

Re: [R] Removing a space from a string

2020-07-28 Thread Richard O'Keefe
The first response has to be "how did the spaces get there in the first place?" Can you fix the process that creates the data? If the process sometimes generates one extra space, are you sure it never generates two? But let's treat this purely as a regular expression problem, where if there is a

Re: [R] Removing a space from a string

2020-07-28 Thread Bert Gunter
Note that my previous strategy can be expressed slightly more clearly as: x <- c("STRING 01. Remainder of the string", "STR ING 01. Remainder of the string", "STRIN G 01. Remainder of the string", "STR IN G 01. Remainder of the string") ## more spaces in this last example entry rx <- "([^[:d

Re: [R] Removing a space from a string

2020-07-28 Thread Bert Gunter
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

Re: [R] Removing a space from a string

2020-07-28 Thread Rasmus Liland
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 st

Re: [R] Removing a space from a string

2020-07-28 Thread cpolwart
This RegEx would do it I think: \s(?=.*\s\d*\.) Looks for space - \s Before any strings followed by space, numbers, period text <- "STR ING 01. Remainder of the string" stringr::str_replace_all(text, "\\s(?=.*\\s\\d*\\.)", "") Should do it I think! On 2020-07-28 21:34, Dennis Fisher wrote:

Re: [R] Removing a space from a string

2020-07-28 Thread Rasmus Liland
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" |

Re: [R] Removing a space from a string

2020-07-28 Thread Dennis Fisher
Only the spaces in STRING. However, if I inadvertently delete the space between STRING and NN, I can add it back in. Dennis Fisher MD P < (The "P Less Than" Company) Phone / Fax: 1-866-PLessThan (1-866-753-7784) www.PLessThan.com > On Jul 28, 2020, at 1:29 PM, cpo

Re: [R] Removing a space from a string

2020-07-28 Thread Dennis Fisher
It is possible that there will be > 1 space. But, most likely only one (i.e., a solution for one space will suffice; a solution for > 1 space would be even better) Dennis Fisher MD P < (The "P Less Than" Company) Phone / Fax: 1-866-PLessThan (1-866-753-7784) www.PLessThan.com

Re: [R] Removing a space from a string

2020-07-28 Thread cpolwart
On 2020-07-28 21:31, Dennis Fisher wrote: Only the spaces in STRING. However, if I inadvertently delete the space between STRING and NN, I can add it back in. Can there only be one space in STR ING or is ST RI NG possible? Dennis Fisher MD P < (The "P Less Than" Company) Phone / Fax: 1-866

Re: [R] Removing a space from a string

2020-07-28 Thread cpolwart
On 2020-07-28 21:20, Dennis Fisher wrote: R 4.0.2 OS X 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. Rem

[R] Removing a space from a string

2020-07-28 Thread Dennis Fisher
R 4.0.2 OS X 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 gener