2

I am new to R and have learned how to geocode a specific address, but I would like to know if it is possible to geocode all pharmacies within a certain area without providing addresses. To start, I tried to geocode Walgreens within a specific zip code where only 1 Walgreens exists. I tried:

test = data.frame(geocode("walgreens, san juan, puerto rico, 00927", "all"))

It returned the location of the walgreens in that zip code. Then I tried a zip code that has 2 walgreens:

test2 = data.frame(geocode("walgreens, san juan, puerto rico, 00925", "all"))

This gives the error:

Error in data.frame(address_components = list(list(long_name = "Walgreens", : arguments imply differing number of rows: 2, 1, 5

Eventually, I would like to find x,y coordinates for all pharmacies within Puerto Rico. Is there a way to have it return multiple x,y coordinates when searching for 'walgreens', 'pharmacy' or other key words?

jazzurro
  • 23,179
  • 35
  • 66
  • 76
Jenna
  • 21
  • 2
  • `geocode("walgreens, san juan, puerto rico, 00927", "all")` returns a list. `data.frame(geocode("walgreens, san juan, puerto rico, 00927", "all"))` returns an error message. The same happened to your second case. I think the key seems to be converting lists to data frames. – jazzurro Apr 19 '15 at 11:56

1 Answers1

0

Here is what I tried. I hope this will help you. geocode("walgreens, san juan, puerto rico, 00927", "all") returned a list. When you specify output = "all" in geocode(), R returns a list. So, data.frame(geocode("walgreens, san juan, puerto rico, 00927", "all") should not work.

I tried to extract longitude and latitude from the list. I am not sure what would be an optimal way to get longitude and latitude from the list. Yet, I have done the following and create a data frame in the end. Seeing the output, it seems that geocode() picked up Walgreens in the neighbourhood. That is, your search picked up Walgreen shops not only in the post code (i.e., 00925) but also in other post codes.

library(ggmap)

### Get geocodes
foo <- geocode(location = "walgreens, san juan, puerto rico, 00927", output = "all")

### Get longitude, latitude, and address from the list

mylist <- lapply(1:length(foo$results), function(x) {

            lat <- foo$results[[x]]$geometry$location$lat
            lon <- foo$results[[x]]$geometry$location$lng
            address <- foo$results[[x]]$formatted_address

            mydata <- cbind(lat, lon, address)
            mydata

})

### Create a data frame.
data.frame(Reduce(rbind, mylist))


#         lat         lon                                                           address
#1 18.4029425 -66.0560009 Walgreens, Ave Universidad, San Juan, San Juan 00927, Puerto Rico
#2  18.397714  -66.004824                                     Walgreens, San Juan, PR 00924
#3  18.370949  -66.067786                                  Walgreens, Rio Piedras, PR 00926
#4  18.369908  -66.058266                                        Walgreens, Cupey, PR 00926
#5  18.376544  -66.046526                                Walgreens, San Juan, PR 00926, USA
jazzurro
  • 23,179
  • 35
  • 66
  • 76