Я пытаюсь получить название страны заданной (широта, долгота), используя coordinates2politics()
из RDSTK
пакета:Получение страны координаты от Lattitude и долготы
library(dplyr)
library(plyr)
library(rjson)
library(RDSTK)
df2 <- df %>%
mutate(politics = coordinates2politics(place_lat, place_lon),
country = ldply(fromJSON(coordinates2politics(place_lat, place_lon)),
data.frame)[["politics.name"]])
Здесь coordinates2politics(latitude, longitude)
возвращает строку JSON который я конвертировать в dataframe для извлечения politics.name
в df2
, я получаю правильное значение politics
(что вся строка JSON), но неверное значение для country
- Используя этот метод (преобразование в dataframe), как я могу получить элемент из строки JSON?
- Будет ли более эффективный метод извлечения элемента из строки JSON?
- Есть ли лучший способ получить название страны с заданной длины латыни/долготы (кроме использования пакета
RDSTK
)?
df1
> dput(head(df1, 10L))
structure(list(place_lat = c(-23.682803, 30.109684, 36.232855,
26.674996, 40.655138, 40.00134, 44.0752271, 32.230987, -9.5333295,
38.3045585), place_lon = c(-46.5955455, -93.767675, -115.223125,
-81.816602, -73.9487755, -74.1880345, -103.2334107, -90.1580165,
-35.6871125, -92.4367735), location = c("South West", "North West",
"North West", "North West", "North West", "North West", "North West",
"North West", "South West", "North West"), sentiment = c("positive",
"positive", "neutral", "positive", "neutral", "positive", "positive",
"neutral", "positive", "neutral"), id = 1:10), .Names = c("place_lat",
"place_lon", "location", "sentiment", "id"), row.names = c(NA,
10L), class = "data.frame")
df2
> dput(head(df2, n=2L))
structure(list(place_lat = c(-23.682803, 30.109684), place_lon = c(-46.5955455,
-93.767675), location = c("South West", "North West"), sentiment = c("positive",
"positive"), id = 1:2, politics = structure(c("[\n {\n \"politics\": [\n {\n \"type\": \"admin2\",\n \"friendly_type\": \"country\",\n \"name\": \"Brazil\",\n \"code\": \"bra\"\n },\n {\n \"type\": \"admin4\",\n \"friendly_type\": \"state\",\n \"name\": \"São Paulo\",\n \"code\": \"br32\"\n }\n ],\n \"location\": {\n \"latitude\": -23.682803,\n \"longitude\": -46.5955455\n }\n }\n]",
"[\n {\n \"politics\": [\n {\n \"type\": \"admin2\",\n \"friendly_type\": \"country\",\n \"name\": \"United States\",\n \"code\": \"usa\"\n },\n {\n \"type\": \"constituency\",\n \"friendly_type\": \"constituency\",\n \"name\": \"Eighth district, TX\",\n \"code\": \"48_08\"\n },\n {\n \"type\": \"admin6\",\n \"friendly_type\": \"county\",\n \"name\": \"Orange\",\n \"code\": \"48_361\"\n },\n {\n \"type\": \"admin4\",\n \"friendly_type\": \"state\",\n \"name\": \"Texas\",\n \"code\": \"us48\"\n },\n {\n \"type\": \"admin5\",\n \"friendly_type\": \"city\",\n \"name\": \"Orange\",\n \"code\": \"48_54132\"\n },\n {\n \"type\": \"admin5\",\n \"friendly_type\": \"city\",\n \"name\": \"Pinehurst\",\n \"code\": \"48_57608\"\n },\n {\n \"type\": \"admin5\",\n \"friendly_type\": \"city\",\n \"name\": \"\",\n \"code\": \"_\"\n }\n ],\n \"location\": {\n \"latitude\": 30.109684,\n \"longitude\": -93.767675\n }\n }\n]"
), .Names = c("http://www.datasciencetoolkit.org/coordinates2politics/-23.682803%2c-46.5955455",
"http://www.datasciencetoolkit.org/coordinates2politics/30.109684%2c-93.767675"
)), country = structure(c(1L, 1L), .Label = "Brazil", class = "factor")), .Names = c("place_lat",
"place_lon", "location", "sentiment", "id", "politics", "country"
), row.names = 1:2, class = "data.frame")