-1

I have to join a few data frames by zip code but one df has the zip code variable values as ( Zip Code 33004). It's typeof is character information. I need to change this variable to keep just the end number (33004) to be able to match up these values to my other dataset.

enter image description here

Leonardo
  • 2,439
  • 33
  • 17
  • 31
  • Relevant old question here - https://stackoverflow.com/questions/14543627/extracting-numbers-from-vectors-of-strings – thelatemail Oct 21 '19 at 22:40
  • [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. That includes a sample of data, all necessary code, and a clear explanation of what you're trying to do and what hasn't worked. – camille Oct 22 '19 at 01:57

1 Answers1

1

Remove all non-digits (\\D OR [^\\d])

x = "Zip Code 33004"
as.numeric(gsub("\\D+", "", x))
#OR
gsub("[^\\d]", "", x, perl = TRUE)
#[1] 33004

Or you can extract first group of 5 digits

sub(".*(\\d{5}).*", "\\1", x)
#[1] 33004
lhjhj
  • 11
  • 1