Formatting Zipcode in R (removing - from zipcode)

115 views Asked by At

I have a dataset of the following:

> head(data,3)
       city          state   zip_code      overall_spend
1      MIDDLESBORO    KY      40965        $252,168.12
2      PALM BEACH     FL      33411-3518   $369,240.74
3      CORBIN         KY      40701        $292,496.03

Now, I want to format the zip_code which has extra parts after -. For example, in the second row, I have 33411-3518. After formatting I want to have only 33411. How can I do this to the whole zip_code column? Also, zip_code is a factor now

1

There are 1 answers

0
akrun On BEST ANSWER

Try

data$zip_code <- sub('-.*', '', data$zip_code)
data$zip_code
#[1] "40965" "33411" "40701"