UK Postcode/String-number trim

Hi,
I have this small data frame:

source <- data.frame(
  stringsAsFactors = FALSE,
               URN = c("VE2238212", "VE874","VE7306","VE21019",
                       "VE215","VE21547","VE25668",
                       "VE21552","VE2133","VE21548","VE21581","VE21512"),
           ROTotal = c(199.21,179,165.58,224.99,
                       149.11,998.56,210.83,735,262.22,167.49,168.21,498.4),
     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.

library(tidyverse)

source %>% mutate(
  pc = str_remove_all(PostcodeShort,
                               "[0-9]"))

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.