I have a column of combined latitude and longitude coordinates in the following format:
我有一列合并的纬度和经度坐标,格式如下:
coordinates
(-73.2566,44.51513)
I am trying to get the format
我想要获得格式
lat long
73.2566 44.51512
I know this seems like a relatively simple question but I cannot seem to find a solution. I have tried using the gsub() function but that does not get rid of the parentheses.
我知道这似乎是一个相对简单的问题,但我似乎无法找到解决方案。我已经尝试使用gsub()函数但是没有摆脱括号。
3 个解决方案
#1
1
> library(stringr)
> library(magrittr)
> "(-73.2566, 44.51513)" %>% # take coord as string
+ str_replace_all("[()]", "") %>% # replace parantheses
+ str_split_fixed(", ", n=2) %>% # split up based on comma and space after
+ as.data.frame %>% # turn this to a data frame
+ transmute(lat=V1, long=V2) # rename the variables
lat long
1 -73.2566 44.51513
If you have a vector of these values, it could easily be adapted by doing this for (i in coords)
where the data.frame
at the end would be repeatedly added to the dataset using something like bind_rows
.
如果你有这些值的向量,可以通过这样做(i in coords)轻松地对其进行调整,其中最后的data.frame将使用类似bind_rows的内容重复添加到数据集中。
#2
2
try with this simple function (need use google api):
尝试使用这个简单的功能(需要使用谷歌API):
google_api <- "https://maps.googleapis.com/maps/api/geocode/json"
geocode <- function(address, verbose=FALSE) {
library(httr)
r <- GET(google_api, query=list(address=address))
stop_for_status(r)
result <- content(r)
first <- result$results[[1]]
df <- as.data.frame(list(lat=first$geometry$location$lat,
lon=first$geometry$location$lng))
return(df)
}
#example
geocode("Suzhou New Matro")
lat lon
1 31.31015 120.6221
Using google api the function geocode()
returns for each city the coordinates in the format you require
使用google api,函数geocode()为每个城市返回所需格式的坐标
#3
1
This might be a wee bit too simple but
这可能有点过于简单了
> coordinates=c(-73.2566,44.51513)
> coordinates
[1] -73.25660 44.51513
> lat=coordinates[1]
> lat
[1] -73.2566
> long=coordinates[2]
> long
[1] 44.51513
There is a ggmap package at https://www.nceas.ucsb.edu/~frazier/RSpatialGuides/ggmap/ggmapCheatsheet.pdf
https://www.nceas.ucsb.edu/~frazier/RSpatialGuides/ggmap/ggmapCheatsheet.pdf上有一个ggmap包。
#1
1
> library(stringr)
> library(magrittr)
> "(-73.2566, 44.51513)" %>% # take coord as string
+ str_replace_all("[()]", "") %>% # replace parantheses
+ str_split_fixed(", ", n=2) %>% # split up based on comma and space after
+ as.data.frame %>% # turn this to a data frame
+ transmute(lat=V1, long=V2) # rename the variables
lat long
1 -73.2566 44.51513
If you have a vector of these values, it could easily be adapted by doing this for (i in coords)
where the data.frame
at the end would be repeatedly added to the dataset using something like bind_rows
.
如果你有这些值的向量,可以通过这样做(i in coords)轻松地对其进行调整,其中最后的data.frame将使用类似bind_rows的内容重复添加到数据集中。
#2
2
try with this simple function (need use google api):
尝试使用这个简单的功能(需要使用谷歌API):
google_api <- "https://maps.googleapis.com/maps/api/geocode/json"
geocode <- function(address, verbose=FALSE) {
library(httr)
r <- GET(google_api, query=list(address=address))
stop_for_status(r)
result <- content(r)
first <- result$results[[1]]
df <- as.data.frame(list(lat=first$geometry$location$lat,
lon=first$geometry$location$lng))
return(df)
}
#example
geocode("Suzhou New Matro")
lat lon
1 31.31015 120.6221
Using google api the function geocode()
returns for each city the coordinates in the format you require
使用google api,函数geocode()为每个城市返回所需格式的坐标
#3
1
This might be a wee bit too simple but
这可能有点过于简单了
> coordinates=c(-73.2566,44.51513)
> coordinates
[1] -73.25660 44.51513
> lat=coordinates[1]
> lat
[1] -73.2566
> long=coordinates[2]
> long
[1] 44.51513
There is a ggmap package at https://www.nceas.ucsb.edu/~frazier/RSpatialGuides/ggmap/ggmapCheatsheet.pdf
https://www.nceas.ucsb.edu/~frazier/RSpatialGuides/ggmap/ggmapCheatsheet.pdf上有一个ggmap包。