In R how to convert Longitude and Latitude to a format that can be used in ggplot2 or ggmap [closed]

拜拜、爱过 提交于 2019-12-01 05:44:54

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/30581838/in-r-how-to-convert-longitude-and-latitude-to-a-format-that-can-be-used-in-ggplo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!