0

I've got a list of Store IDs and their Zipcodes in a 2 column numeric vector (in R). I'm using the "Zipcode" package (https://cran.rproject.org/web/packages/zipcode/zipcode.pdf) and have access to the longitude/latitude coordinates for these zipcodes. The zipcode package has a data frame with every zip code, city,state, and longitude and latitude for all the zipcodes (as a large dataframe).

I'm looking to get the longitude and latitude coordinates for my Zipcodes, and add them as columns 3 and 4 (i.e. Store ID, Zip Code, Longtitude, Latitude)

Any thoughts? Thank you!

EDIT: I've tried the merge function (i.e.) total<-merged(CleanData,zipcode, by=zip) and I'm getting an error because they must have the same number of columns?

2 Answers2

0

The column name passed as the by argument has to be enclosed within quotes. You don't need the by argument in merge in this example, if zipcode is the only common column in the two dataframes.

Example datasets:

#cleanData
d1<-tibble::tribble(~z,~id,131,1,114,2,155,5)

#zipcode
d2<-
tibble::tribble(~z,~x,~y,131,2,5,166,2,6,162,6,5,177,7,1,114,2,1,155,5,9)

result <- merge(d1,d2)

gives

       z id x y
    1 114  2 2 1
    2 131  1 2 5
    3 155  5 5 9

You can remove any unnecessary columns from the result dataframe by simply using dplyr::select(). Suppose you don't need column y (which may be a state name, for example)

result <- dplyr::select(result, z, id, x)
Raj Padmanabhan
  • 540
  • 5
  • 11
0

Ended up using this: How to join (merge) data frames (inner, outer, left, right)?

essentially I used the Left Outer function because I wanted to keep all of the zipcodes in my store database. I believe the answer above would eliminate zipcodes not found in the second list of zipcodes.