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

白昼怎懂夜的黑 提交于 2019-12-01 06:12:15

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