Search code examples
rgpslatitude-longitude

How do I convert Latitude and Longitude coordinates to GPS (NS/EW) coordinates in R


Apologies if I'm not using the correct terminology. But I am looking for a way to convert geocoded Latitude and Longitude to a Degree format which shows GPS (NS/EW) coordinates in R.

Some Examples

1. Toronto, ON, Canada

Geocoded Longitude and Latitude (From latlong.net)

lat          lon
43.651070    -79.347015

GPS Coordinates (From Google)

43.6532° N, 79.3832° W

2. Melbourne Australia

Geocoded Longitude and Latitude (From latlong.net)

lat          lon
-37.840935   144.946457

GPS Coordinates (From Google)

37.8136° S, 144.9631° E

Solution

  • sample_data = data.frame(
      lat = c(43.651070, -37.840935),
      lon = c(-79.347015, 144.946457)
    )
    
    with(sample_data, paste0(
      abs(lat), "° ", ifelse(lat > 0, "N", "S"), ", ",
      abs(lon), "° ", ifelse(lon > 0, "E", "W")
    ))
    # [1] "43.65107° N, 79.347015° W"   "37.840935° S, 144.946457° E"