Search code examples
rgeocode

Using geocode on a column of addresses - Not recognising them as a location


I'm very new to coding and have a project on data science due in the next 2 weeks. Rstudio won't recognize a column of Locations named "AdminPort" as locations when I use the geocode function. What have I done wrong?

# A tibble: 31 x 2
   AdminPort       n
   <fct>       <int>
 1 ABERDEEN       70
 2 AYR            77
 3 BELFAST       187
 4 BRIXHAM       184
 5 BUCKIE         69
 6 CAMPBELTOWN    97
 7 EYEMOUTH       73
 8 FLEETWOOD      92
 9 FRASERBURGH   120
10 GRIMSBY        56
# ... with 21 more rows
geocode(AdminPort, source = "dsk")

Solution

  • The geocode function from ggmap package expects a character vector as input of location, AdminPort column is a factor, you should convert it to character before using geocode. Assuming your dataframe is called df :

    df$AdminPort <- as.character(df$AdminPort)
    

    Afterwards you can call the geocode function with location=df$AdminPort