1

I am trying to convert my data so that it can be plotting on a map. For example the data looks like:

# A tibble: 2 x 2
  Latitud           Longitud        
  <chr>             <chr>           
1 10º 35' 28.98'' N 3º 41' 33.91'' O
2 10º 35' 12.63'' N 3º 45' 46.22'' O

I am trying to mutate it using the following:

df %>% 
  mutate(
    Latitud = str_replace_all(Latitud, "''", ""),
    lat_edit = sp::char2dms(Latitud), "°")

Which returns and error:

Error in if (any(abs(object@deg) > 90)) return("abs(degree) > 90") : 
  missing value where TRUE/FALSE needed
In addition: Warning message:
In asMethod(object) : NAs introduced by coercion

I would like to plot these two points on a map in ggplot (or another spatial package)

Data:

structure(list(Latitud = c("40º 25' 25.98'' N", "40º 25' 17.63'' N"
), Longitud = c("3º 42' 43.91'' O", "3º 40' 56.22'' O")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -2L))
M--
  • 25,431
  • 8
  • 61
  • 93
user8959427
  • 2,027
  • 9
  • 20

2 Answers2

6

You can use the following custom function (I am assuming N, S, W, E. Not sure what O means in longitude):

angle2dec <- function(angle) {
  angle <- as.character(angle)
  angle <- ifelse(grepl("S|W", angle), paste0("-", angle), angle)
  angle <- trimws(gsub("[^- +.0-9]", "", angle))
  x <- do.call(rbind, strsplit(angle, split=' '))
  x <- apply(x, 1L, function(y) {
    y <- as.numeric(y)
    (abs(y[1]) + y[2]/60 + y[3]/3600) * sign(y[1])
  })
  return(x)
}

Applying on the data:

df1[] <- lapply(df1, angle2dec)

df1
#>     Latitud  Longitud
#> 1 -40.42388  3.712197
#> 2  40.42156 -3.682283

Plotting:

library(ggplot2)

ggplot(df1, aes(x = Longitud, y = Latitud)) +
  geom_point()


Slightly Modified Data to Show for Different Hemispheres:

df1 <- structure(list(Latitud = c("40<U+623C><U+3E61> 25' 25.98'' S", 
                                  "40<U+623C><U+3E61> 25' 17.63'' N"), 
                      Longitud = c("3<U+623C><U+3E61> 42' 43.91'' E",
                                   "3<U+623C><U+3E61> 40' 56.22'' W")), 
                 class = c("tbl_df", "tbl", "data.frame"), 
                 row.names = c(NA, -2L))

In reference to Converting geo coordinates from degree to decimal .

M--
  • 25,431
  • 8
  • 61
  • 93
  • 1
    Thanks! it works on my full dataset also. I will plot them and double check the locations, thanks again! – user8959427 Oct 31 '19 at 15:35
  • 1
    Thanks! I will take a closer look at the answers today and accept one of them. `N` - Norte, `S` - Sur, `W` - Oeste and `E` - Este in Spanish. I will change them to English however. – user8959427 Oct 31 '19 at 21:39
2

I'll preface this by saying I hadn't used char2dms until right now, so there may be intricacies I missed (such as my question above about "O" as a direction). Looking at the docs and examples, you need to give the characters used to demarcate degrees, minutes, and seconds. In your case, these are "º", "'", and "''", respectively. I skipped the step of removing the third of these, because it's necessary to see where the seconds are written. (Update: added a step to replace the regex "O$" (oeste) with "W" (west)). That gets you what's below:

library(dplyr)
library(ggplot2)
library(sp)

dat <- structure(list(Latitud = c("40º 25' 25.98'' N", "40º 25' 17.63'' N"
), Longitud = c("3º 42' 43.91'' O", "3º 40' 56.22'' O")), class = c("tbl_df", 
                                                                    "tbl", "data.frame"), row.names = c(NA, -2L)) %>%
  mutate_at(vars(Latitud, Longitud), stringr::str_replace_all, "O$", "W")

char2dms(dat$Latitud, chd = "º", chm = "'", chs = "''")
#> [1] 40d25'25.98"N 40d25'17.63"N

This is a DMS S3 object, not a vector (here's where my knowledge of this ends), so you can't put it directly into the data frame columns. Instead, convert to a numeric vector, and you've got numeric coordinates in your data frame.

dat_numeric <- dat %>%
  mutate(lat_edit = as.numeric(char2dms(dat$Latitud, chd = "º", chm = "'", chs = "''")),
         lon_edit = as.numeric(char2dms(dat$Longitud, chd = "º", chm = "'", chs = "''")))

dat_numeric
#> # A tibble: 2 x 4
#>   Latitud           Longitud         lat_edit lon_edit
#>   <chr>             <chr>               <dbl>    <dbl>
#> 1 40º 25' 25.98'' N 3º 42' 43.91'' W     40.4    -3.71
#> 2 40º 25' 17.63'' N 3º 40' 56.22'' W     40.4    -3.68

Plot like normal numbers:

ggplot(dat_numeric, aes(x = lon_edit, y = lat_edit)) +
  geom_point()

Or convert to an sf object and plot with the appropriate aspect ratio, projection, etc.

sf::st_as_sf(dat_numeric, coords = c("lon_edit", "lat_edit")) %>%
  ggplot() +
  geom_sf()

enter image description here

camille
  • 16,432
  • 18
  • 38
  • 60