How to use str_squish on a column of postcodes

I have a set of postcodes in one column in a large dataset. I want to take out the middle white space, e.g I would like "ABC 123" to be "ABC123". I've tried using str_squish and I can't get it to work. Can someone suggest the correct usage for the code below please or another method - many thanks.

#install.packages("tidyverse")
library(tidyverse)

df <- tibble(
  Postcode = c("ABC 123", "ABC 124", "CDE 125"),
  Area = c(1:3)
  )


df %>% 
  str_squish(Postcode)

Ah, the delights of our UK postcode system:

Try this:

df %>% 
  mutate(Postcode = str_replace(Postcode, " ", ""))
5 Likes

You are a postcode lifesaver - been at this for two hours, dear me. Thanks Martin!

Use str_replace_all() if you have more than one space.

This can easily happen depending on the source of the raw data and the irregular postcode format.

2 Likes

By the way, the reason str_squish() didn’t work is that it’s solving a slightly different problem. It only removes repeated internal whitespace, so it will turn 2+ spaces into 1, but not 1 space into 0. For example (shamelessly lifted from the documentation):

str_squish("  String with trailing,  middle, and leading white space\t")
#> [1] "String with trailing, middle, and leading white space"
2 Likes