0
ds<- read.csv("https://gd.eppo.int/taxon/SIRXNO/download/distribution_csv")
str(ds)
# 'data.frame': 54 obs. of  6 variables:
#  $ continent   : chr  "Africa" "Africa" "Africa" "Africa" ...
#  $ country     : chr  "Algeria" "Morocco" "South Africa" "Tunisia" ...
#  $ state       : chr  "" "" "" "" ...
#  $ country.code: chr  "DZ" "MA" "ZA" "TN" ...
#  $ state.code  : chr  "" "" "" "" ...
#  $ Status      : chr  "Present, no details" "Present, no details" "Present, no details" "Present, widespread" ...

unique(ds$country)
#  [1] "Algeria"                  "Morocco"                  "South Africa"            
#  [4] "Tunisia"                  "Argentina"                "Brazil"                  
#  [7] "Canada"                   "Chile"                    "United States of America"
# [10] "Uruguay"                  "Mongolia"                 "Austria"                 
# [13] "Belgium"                  "Cyprus"                   "Denmark"                 
# [16] "Estonia"                  "Finland"                  "France"                  
# [19] "Georgia"                  "Germany"                  "Greece"                  
# [22] "Hungary"                  "Italy"                    "Norway"                  
# [25] "Poland"                   "Portugal"                 "Romania"                 
# [28] "Russia"                   "Serbia"                   "Slovenia"                
# [31] "Spain"                    "Switzerland"              "United Kingdom"          
# [34] "Australia"                "New Zealand"   
unique(ds$state)
#  [1] ""                  "Parana"            "Rio Grande do Sul" "Santa Catarina"   
#  [5] "Ontario"           "Connecticut"       "Michigan"          "New York"         
#  [9] "Ohio"              "Pennsylvania"      "Vermont"           "Azores"           
# [13] "Madeira"           "Eastern Siberia"   "Western Siberia"   "Islas Canárias"  
# [17] "New South Wales"   "South Australia"   "Tasmania"          "Victoria" 

I'd like to get the latlong coordinates for the state if have the state entry, if not the country coordinate. My desirable output would be something like this (not coordinates true, just to show the idea):

head(ds_coords)
  continent      country state country.code state.code                           Status  lat  long
1    Africa      Algeria                 DZ                         Present, no details  28.0339 1.6596
2    Africa      Morocco                 MA                         Present, no details  31.7917 -7.0926
3    Africa South Africa                 ZA                         Present, no details  -30.5595 22.9375
4    Africa      Tunisia                 TN                         Present, widespread  33.8869 9.5375
5   America    Argentina                 AR            Present, restricted distribution  -38.4161 -63.6167
6   America       Brazil                 BR            Present, restricted distribution  -14.2350 -51.9253
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Leprechault
  • 1,531
  • 12
  • 28
  • Leprechault, apologies to be blunt, but I have difficulties understanding what your problem is. If `head(ds_coords)` gives you already an output that is "... something like this ...", what is it that you do not have or cannot master? What have you tried so far? What is not working? – Ray Nov 01 '22 at 15:31
  • 1
    Leprechault, are you asking to find a data source to translate a "city or country" into coordinates? Have you looked at `maps`/`mapdata` and/or google api? – r2evans Nov 01 '22 at 15:33
  • @Ray I don't try anything yet, I just googling convert country/state/city names to coordinates using R. – Leprechault Nov 01 '22 at 15:40
  • I thank @r2evans to gave me some direction on my problem. – Leprechault Nov 01 '22 at 15:40
  • 1
    Relevant, from [`site:stackoverflow.com "r" coordinates from country`](https://www.google.com/search?q=site%3Astackoverflow.com+"r"+coordinates+from+country): https://stackoverflow.com/q/13905098/3358272, https://stackoverflow.com/q/67244802/3358272, https://stackoverflow.com/q/67073913/3358272 – r2evans Nov 01 '22 at 15:43
  • 2
    @r2evans ... gee you beat me :) ... Leprechault, you should find several data sets with LAT/LON for countries, just hacking this into google gave me (on top of stackoverflow): https://www.kaggle.com/datasets/paultimothymooney/latitude-and-longitude-for-every-country-and-state or https://developers.google.com/public-data/docs/canonical/countries_csv – Ray Nov 01 '22 at 15:50
  • In R try `tidygeocoder` or `nominatimlite` packages to geocode addresses. The output would be your input plus the lon/lat values, see https://jessecambon.github.io/tidygeocoder/articles/tidygeocoder.html – dieghernan Nov 03 '22 at 18:42

0 Answers0