4

There are a slew of answers out there on how to remove extra whitespace from between words, which is super simple. However, I'm finding that removing extra whitespace within words is much harder. As a reproducible example, let's say I have a vector of data that looks like this:

x <- c("L L C", "P O BOX 123456", "NEW YORK")

What I'd like to do is something like this:

y <- gsub("(\\w)(\\s)(\\w)(\\s)", "\\1\\3", x)

But that leaves me with this:

[1] "LLC" "POBOX 123456" "NEW YORK"

Almost perfect, but I'd really like to have that second value say "PO BOX 123456". Is there a better way to do this than what I'm doing?

1 Answer 1

8

You may try this,

> x <- c("L L C", "P O BOX 123456", "NEW YORK")
> gsub("(?<=\\b\\w)\\s(?=\\w\\b)", "", x,perl=T)
[1] "LLC"           "PO BOX 123456" "NEW YORK" 

It just removes the space which exists between two single word characters.

1
  • 1
    Simply amazing. Nice work, my friend! I'll accept in 8 minutes when I can.
    – tblznbits
    Jul 7, 2015 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.