Reputation: 29
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.
Upvotes: 0
Views: 564
Reputation: 11955
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
Upvotes: 1