0

Im having a problem trying to move data from my address column to my postal code column. For example:

enter image description here

On the second line im trying to take "Dublin 22" from the data.Address column and moving it to the data.Postal.Code column.

Im using R but i have no idea how to implement it.

Any suggestions?

Frank
  • 66,179
  • 8
  • 96
  • 180
  • 1
    Please update your question and show several example addresses and where the postal code appears. – Tim Biegeleisen Feb 13 '17 at 15:35
  • 3
    Welcome to SO. Please have a read at [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and of course how to [make a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). In addition, please add expected output as well as the code you have tried and failed. – Sotos Feb 13 '17 at 15:35

1 Answers1

0

Try this:

data.Postal.Code <- gsub("^.*, (.*)$", "\\1", data.Address)

Update:

If you want to move Dublin 22 to the postal code column whenever it appears in the address then you can try the following:

data.Postal.Code[grepl("^.* Dublin 22$", data.Address)] <- "Dublin 22"

Here is a demo of the regex used:

Regex101

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360