rgeocodingr-maptools

how to get geocode in R?


i have tried 2 approaches to get the long lat of location.

install.packages("maptools")
library(maptools)
A1 <- data.frame(A1= c("Dordrecht", "Berlin", "New York", 
                                    "Batu Pahat", "Edinburgh"), 
                                    stringsAsFactors = FALSE)

result <- lapply(A1[, 1], geocode_OSM)

or


A2_df <- mutate_geocode(A1, loc)

but both do not work

        loc
  1    Dordrecht
  2    Berlin
  3    New York
  4    Batu Pahat 
  5    Edinburgh  

any ideas to get the respectively code without API key?


Solution

  • Is this what you are looking for?

    library(tmaptools)
    
    geocode_OSM(A1$A1)
    
    #>        query       lat        lon   lat_min   lat_max    lon_min    lon_max
    #> 1  Dordrecht 51.795881   4.677935 51.714123 51.823358   4.620404   4.884643
    #> 2     Berlin 52.517037  13.388860 52.357036 52.677036  13.228860  13.548860
    #> 3   New York 40.712728 -74.006015 40.477399 40.916179 -74.259090 -73.700181
    #> 4 Batu Pahat  1.933333 103.000000  1.616728  2.200501 102.774169 103.256164
    #> 5  Edinburgh 55.953346  -3.188375 55.818792 56.004084  -3.449533  -3.074951
    

    data

    A1 <- data.frame(A1= c("Dordrecht", "Berlin", "New York", 
                           "Batu Pahat", "Edinburgh"), 
                     stringsAsFactors = FALSE)
    

    Created on 2021-03-30 by the reprex package (v1.0.0)