'Conditional column value calculation, zip code to lat/long

I need help trying to figure out how to do this in R. I don't know how to generate latitude and longitude from zip code data so I am stuck on the first part.

I have a df that looks like:

ID ZIP code loc 1 zip code loc 2 zip code loc 3 zip code Opinion on loc 1 Opinion on loc 2 Opinion on loc 3
1 90804 91723 90814 91604 Neutral Like Dislike

I am trying to add three columns that look like this:

ID ZIP code loc 1 zip code loc 2 zip code loc 3 zip code Opinion on loc 1 Opinion on loc 2 Opinion on loc 3 Distance from Like Distance from Disike Distance from Neutral
1 90804 91723 90814 91604 Neutral Like Dislike 42 Km 56 Km 24 Km

I only have the zip code data, I am trying to know if there is a way to create columns for longitude and latitude so that distance calculation is possible.

After that, the columns that need to be created need to first match which location does the customer 'Like', calculate their distance from it, and then put the distance in the 'Distance from Like' column and so on.



Solution 1:[1]

If it are USA zipcodes, use library zipcodeR which directly allows you to calculate distances between zipcodes

library(zipcodeR)
zip_distance(zipcode_a, zipcode_b, lonlat = TRUE, units = "meters")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Merijn van Tilborg