Kitty
Kitty

Reputation: 21

How to remove unwanted space between words inside a character vector using R?

I have a character vector like:

"I t is tim e to g o"

I wanted it to be:

"It is time to go"

Upvotes: 1

Views: 1053

Answers (2)

vks
vks

Reputation: 67968

This regex works in your case: "\\s(?=\\S\\s\\S{2,}|\\S$)"

string <- "I t is tim e to g o"
gsub("\\s(?=\\S\\s\\S{2,}|\\S$)", "", string, perl=TRUE)

## [1] "It is time to go"

Try this.Replace by empty string. See demo.

https://regex101.com/r/nL5yL3/32

Upvotes: 2

Jim
Jim

Reputation: 4767

Using rex may make this type of task a little simpler. Although in this case maybe not :)

string <- "I t is tim e to g o"

library(rex)
re_substitutes(string, rex(
    space %if_next_is%
      list(
        list(non_space, space, at_least(non_space, 2)) %or%
        list(non_space, end)
      )
    ), "", global = TRUE)

#> [1] "It is time to go"

Upvotes: 0

Related Questions