crableau
crableau

Reputation: 1

Converting zipcodes to state in R using reverse_zipcode() outputs in the wrong order

I am trying to change a set of zipcodes into states. However, the result comes in a different order than what I inputed, except for null values. This is a different set I created, which produces the same issue. I'm importing my actual file from a CSV if that is relevant.

I'm using the zipcodeR package.

zipcodestest = as.data.frame(c('85364','91910','30004','filler','90210','help'))

colnames(zipcodestest) = "zip"

statetest =as.data.frame(reverse_zipcode(zipcodestest$zip)$state)

zipcodestest$statetest = statetest

View(zipcodestest)

The states are showing up in a different order than the zips. Is there a way I can make sure they pair up properly? Thanks so much.

Upvotes: 0

Views: 569

Answers (1)

Onyambu
Onyambu

Reputation: 79358

zipcodestest %>%
   left_join(reverse_zipcode(.$zip),
             by = c(zip ='zipcode')) %>%
   select(zip, state)
     zip state
1  85364    AZ
2  91910    CA
3  30004    GA
4 filler  <NA>
5  90210    CA
6   help  <NA>

Upvotes: 1

Related Questions