当值为数字时,如何将纬度和小数分钟重新计算为十进制度数?
How to recalculate latitude and decimal minutes to decimal degrees when values are numeric?
我在 R 工作。
我有一个带有纬度和经度的 df,读入为:
lat long
5542.780 1204.000
5540.463 1005.425
5639.760 958.420
etc.
其中纬度为 55 度,42.780 为十进制分钟。我想通过输出将其转换为十进制度数:
lat long
55.713 12.06667
55.67438 10.09042
56.66267 9.973667
etc.
我知道这可以通过例如55 + 42.780/60 = 55.713。但我不知道如何为 R 中的整个 df 自动执行此操作,它有大约 79 000 个观察结果:)这一定是一种方法,我已经搜索但找不到解决方案。
我只是实现了 post 中提到的计算,以便在完整的数据帧上进行转换。希望这对您有所帮助!
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
输出为:
lat long
[1,] 55.71300 12.066667
[2,] 55.67438 10.090417
[3,] 56.66267 9.973667
我在 R 工作。 我有一个带有纬度和经度的 df,读入为:
lat long
5542.780 1204.000
5540.463 1005.425
5639.760 958.420
etc.
其中纬度为 55 度,42.780 为十进制分钟。我想通过输出将其转换为十进制度数:
lat long
55.713 12.06667
55.67438 10.09042
56.66267 9.973667
etc.
我知道这可以通过例如55 + 42.780/60 = 55.713。但我不知道如何为 R 中的整个 df 自动执行此操作,它有大约 79 000 个观察结果:)这一定是一种方法,我已经搜索但找不到解决方案。
我只是实现了 post 中提到的计算,以便在完整的数据帧上进行转换。希望这对您有所帮助!
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
输出为:
lat long
[1,] 55.71300 12.066667
[2,] 55.67438 10.090417
[3,] 56.66267 9.973667