I have a dataframe of >10,000 rows. Column c is the column containing the full address in string, including the postal code. I would like to extract the postal code digits (6 digits) into a new column. All 6-digit postal codes come after the word, Singapore.
An example is as follows:
df <- c(a,b,c)
c <- c("YVL WELLNESS CLINIC 510 CAMDEN STREET #01-01, Singapore 248180", "MOMO CLINIC 512 CHOA CHU KANG STREET, #10-1102, Singapore 150902",...)
# need to extract 6-digit postal codes in c, into a new column, d
How do I extract the 6 digit postal codes into a new column, d?
Thank you!
Use
str_extract:The regex pattern here is simply for any 6-digit string. If you have cases where such strings occur that are not postal codes you can refine the pattern using contextual information around the codes. For example it appears that the postal codes always occur at the end of the string. That end-of-string position can be targeted by the anchor
$, like so:\\d{6}$Data: