问题描述
我得到了原始的经度/纬度数据,其格式无法用R图处理。所以我想知道是否有一些R函数或算法可以帮助我将这些原始数据转换为可读格式。 (也许是UTM)以下是我的原始数据:
Latitude:
32-14-23 N31 -04-27 N33-45-28 N
经度:
121-22-38 W 119-30-05 W122-47-52 W
我希望将格式转换为(下面的示例是模拟数据):
经度: -2.748
纬度 53.39
看起来您有度数/分钟/秒格式的数据,并且您想要它以十进制格式。您可以使用 sp
包中的 char2dms
函数从字符串转换为度分秒对象,然后使用 as.numeric
转换为十进制
示例:
> as.numeric(sp :: char2dms(32d14'23 \N))
[1] 32.23972
> as.numeric(sp :: char2dms(121d22'38 \ W))
[1] -121.3772
另请注意 char2dms
正在寻找像 32d14'23N
,而不是 32-14-23 N $ c $使用
magrittr
管道链:
32-14-23 N%>%
sub(' - ','d',。)%>%
sub(' - ','\\ '',。)%>%
sub('',''',。,%>%
char2dms%>%
as.numeric
I got a raw Longitude/Latitude data whose format cannot be processed by R map. So I wonder if there are some R functions or algorithms to help me to convert those raw data into a readable format. (Maybe UTM) Here is the raw data I had:
Latitude:"32-14-23 N" "31-04-27 N" "33-45-28 N"
Longitude:"121-22-38 W" "119-30-05 W" "122-47-52 W"
I wish to convert the format as(the sample below is mock data):
Longitude: -2.748
Latitude: 53.39
Looks like you have data in degree / minute / second format and you want it in decimal format. You can use the char2dms
function in the sp
package to go from character string to a degree-minute-second object, and then use as.numeric
to convert to decimal
Example:
> as.numeric(sp::char2dms("32d14'23\"N"))
[1] 32.23972
> as.numeric(sp::char2dms("121d22'38\" W"))
[1] -121.3772
Also note char2dms
is looking for something like 32d14'23" N
, not 32-14-23 N
, so you may have to assemble the proper string. Using a magrittr
pipe chain:
"32-14-23 N" %>%
sub('-', 'd', .) %>%
sub('-', '\'', .) %>%
sub(' ', '" ', .) %>%
char2dms %>%
as.numeric
这篇关于在R如何将经度和纬度转换为可以在ggplot2或ggmap中使用的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!