title: Zip code distance R
body: <p>I'm using the Zip Code package in R, and I'd like to make a list of all zip codes that are within a 10, 20, or X mile radius from each zip code. From there I will roll up zip code data to a 10, 20, or X mile total. I'm currently joining every single zip code, with each zip code (so the number of rows squares). Then calculating the distance between each zip code. And then eliminating the distances where greater than 10,20, X miles. Is there a better way to do this in R so I do not have to calculate all possibilities? I'm brand new to R. Thanks!</p>
  
  <pre><code>Code is here:
  #Bringing in Zipcode database. 
  library(zipcode)
  data(zipcode)
  
  #Limiting to certain states that I want to include,
  SEZips &lt;- zipcode[zipcode$state %in% c("GA","AL", "SC", "NC"),]
  
  #Duplicating the data set to join it together
  SEZips2 &lt;- SEZips
  
  #To code in SQL
  library(sqldf)
  
  #Creating a common match so I can join all rows from both tables together
  SEZips$Match &lt;- 1
  SEZips2$Match &lt;- 1
  
  #attaches every zip code to each zip
  ZipList &lt;- sqldf("
                   SELECT
                   A.zip as zip1,
                   A.longitude as lon1,
                   A.latitude as lat1,
                   B.zip as zip2,
                   B.longitude as lon2,
                   B.latitude as lat2
                   From SEZips A
                   Left Join SEZips2 B
                   on A.Match = B.Match
                   ")
  
  
  #to get the distance calculation, use package geosphere, 
  library(geosphere)
  
  #radius of Earth in miles, adjust for km, etc.
  r = 3959
  #Creating Table of the coordinates. Makes it easy to calc distance
  Points1 &lt;- cbind(ZipList$lon1,ZipList$lat1)
  Points2 &lt;- cbind(ZipList$lon2,ZipList$lat2)
  distance &lt;- distHaversine(Points1,Points2,r)
  
  #Adding distance back on to the original ZipList
  ZipList$Distance &lt;- distance
  
  #To limit to a certain radius.E.g. 15 for 15 miles.
  z = 15
  #Eliminating matches &gt; z 
  ZipList2 &lt;- ZipList[ZipList$Distance &lt;= z,]
  
  #Adding data to roll up, e.g. population
  ZipPayroll &lt;- read.csv("filepath/ZipPayroll.csv")
  
  #Changin Zip to 5 character from integer. A little bit of pain
  #Essentailly code says (add 5 0's, and then grab the right 5 characters)
  ZipPayroll$Zip2 &lt;- substr(paste("00000",ZipPayroll$zip,sep=""),nchar(paste("00000",ZipPayroll$zip,sep=""))-4,nchar(paste("00000",ZipPayroll$zip,sep="")))
  
  #Joining Payroll info to SEZips dataframe
  SEZips &lt;- sqldf("
                  SELECT
                  A.*,
                  B.Payroll, 
                  B.Employees,
                  B.Establishments
                  From SEZips A
                  Left Join ZipPayroll B
                  on A.zip = B.Zip2
                  ")
  
  #Rolling up to 15 mile level
  SEZips15 &lt;- sqldf("
                    SELECT
                    A.zip1 as Zip, 
                    Sum(B.Payroll) as PayrollArea,
                    Sum(B.Employees) as EmployeesArea,
                    Sum(B.Establishments) as EstablishmentsArea
                    From ZipList2 A
                    Left Join SEZips B
                    on A.zip2 = B.zip
                    Group By A.zip1
                    ")
  
  #Include the oringinal Zip data 
  SEZips15 &lt;- sqldf("
                    SELECT
                    A.*,
                    B.Payroll,
                    B.Employees,
                    B.Establishments as EstablishmentsArea
                    From SEZips15 A
                    Left Join SEZips B
                    on A.zip = B.zip
                    ")
  
  #Calculate Average Pay for Zip and Area
  SEZips15$AvgPayArea &lt;- SEZips15$PayrollArea / SEZips15$EmployeesArea
  SEZips15$AvgPay &lt;- SEZips15$Payroll / SEZips15$Employees
  </code></pre>