0

I am working in R. I have a df with lat and long read in as:

     lat        long
     5542.780  1204.000 
     5540.463  1005.425
     5639.760   958.420
     etc. 

Where latitude is 55 degrees and 42.780 is decimal minutes. I want to transform this into decimal degrees with output:

     lat        long
     55.713    12.06667
     55.67438  10.09042
     56.66267   9.973667
     etc.

I know that this can be calculated by e.g. 55 + 42.780/60 = 55.713. But I don´t know how to do it automatically for the whole df within R, which has about 79 000 observations :) it must be a way, I have searched but cannot find the solution.

  • 1
    Possible duplicate of [Converting geo coordinates from degree to decimal](https://stackoverflow.com/questions/14404596/converting-geo-coordinates-from-degree-to-decimal) – amonk Sep 15 '17 at 12:38
  • https://stackoverflow.com/questions/14404596/converting-geo-coordinates-from-degree-to-decimal – amonk Sep 15 '17 at 12:38
  • Thanks, but no. As it is using a function to split the data at fixed symbols, I tried to adjust it for my data but it did not work. I needed a code that adjusts where to split the degrees data, provided below! – E. Therese Harvey Sep 19 '17 at 11:17

1 Answers1

1

I simply implemented your calculation mentioned in the post to have the conversion on complete dataframe. Hope this helps!

df <- read.table(text="lat        long
5542.780  1204.000 
5540.463  1005.425
5639.760   958.420", header=T, sep="")
df

df_converted <- sapply(df, function(x) 
  as.numeric(gsub("(.*)(\\d{2}\\.\\d+)", "\\1", formatC(as.numeric(x),format='f',digits=3,flag='0'))) + 
    (as.numeric(gsub("(.*)(\\d{2}\\.\\d+)", "\\2", formatC(as.numeric(x),format='f',digits=3,flag='0')))/ 60))
df_converted

Output is:

          lat      long
[1,] 55.71300 12.066667
[2,] 55.67438 10.090417
[3,] 56.66267  9.973667
Prem
  • 11,775
  • 1
  • 19
  • 33
  • It works excellent! Thank you very much! I needed it to be adjustable for 1 or 2 digits in the degree number, which really made it tricky. Also now marked as correct, – E. Therese Harvey Sep 19 '17 at 11:20