How load and transform extrange coordinates data

Hi community

Im try to load this .xlsx but the numbers is NA. Im try to change with digits options, and other libraries but is the same situation. Only get NA number but are 6 with separate by dot between each 2 number, like this:
NEW_LATITUD | NEW_LONGITUD

  • | 41.27.01 | 22.01.40
  • | 41.27.45 | 22.01.51

but in Rstudio this are load like NA.

When check the structure of this colums shows this :thinking:

<lgl>

The format of this coordinates are vey estrange, I need change in a well format from transform to decimal degrees.

The idea is get this friendly format for use this page for transform all coordinates:
https://data.canadensys.net/tools/coordinates

file:

Tnks.

If you want to convert the columns that are in DD.MM.SS into decimal degrees, you can do it like this:

DF <- openxlsx::read.xlsx("~/R/Play/exampledata.xlsx")
DF
#>   LATITUD NEW_LATITUD LONGITUD NEW_LONGITUD
#> 1   42.02    41.59.48    20.95     20.58.55
#> 2   41.13    41.06.50    20.78     20.47.31
#> 3   41.13    41.06.50    20.78     20.47.31
#> 4   42.06    41.59.56    21.43     21.25.29
#> 5   41.13    41.06.50    20.80     20.47.31
str(DF)
#> 'data.frame':    5 obs. of  4 variables:
#>  $ LATITUD     : num  42 41.1 41.1 42.1 41.1
#>  $ NEW_LATITUD : chr  "41.59.48" "41.06.50" "41.06.50" "41.59.56" ...
#>  $ LONGITUD    : num  20.9 20.8 20.8 21.4 20.8
#>  $ NEW_LONGITUD: chr  "20.58.55" "20.47.31" "20.47.31" "21.25.29" ...
library(tidyr)
library(dplyr)

DF <- DF |> separate(col = "NEW_LATITUD", into = c("Lat_Deg","Lat_Min","Lat_Sec")) |> 
  separate(col = "NEW_LONGITUD", into = c("Lng_Deg","Lng_Min","Lng_Sec")) |> 
  mutate(across(.cols = where(is.character), .fns = as.numeric)) |> 
  mutate(NewLat = Lat_Deg + Lat_Min/60 + Lat_Sec/3600,
         NewLng = Lng_Deg + Lng_Min/60 + Lng_Sec/3600) |> 
  select(LATITUD,NewLat, LONGITUD, NewLng)
DF
#>   LATITUD   NewLat LONGITUD   NewLng
#> 1   42.02 41.99667    20.95 20.98194
#> 2   41.13 41.11389    20.78 20.79194
#> 3   41.13 41.11389    20.78 20.79194
#> 4   42.06 41.99889    21.43 21.42472
#> 5   41.13 41.11389    20.80 20.79194

Created on 2023-01-24 with reprex v2.0.2

1 Like

Was the solution. Im break my brain think how to put degree° minute' second". Im thinked with regular expression but this solution was very usefull.
Tnks!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.