UK Postcode/String-number trim

I have this small data frame:

source <- data.frame(
  stringsAsFactors = FALSE,
               URN = c("VE2238212", "VE874","VE7306","VE21019",
           ROTotal = c(199.21,179,165.58,224.99,
     PostcodeShort = c("AB33","AL8","B15","B16",
                       "B24","BA1","BA12","W10","W13","W8 7","WA10","WA11")

where I would like to add one more variable with just prefix letters from the PostcodeShort.
Selecting first wo characters is not working as I should have B and BA, W and WA (not B1, B2, BA, W1, W8 ,WA).

Can you help?

In other words you want to drop numeric digits ?

Yes, please. I need to keep only first letters (one or two) and drop numbers.


source %>% mutate(
  pc = str_remove_all(PostcodeShort,

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.