2017-09-22 19 views
0

JSONファイルをRに(http://eric.clst.org/wupl/Stuff/gz_2010_us_040_00_20m.json)からインポートしました。カンザスで唯一の郡を選択しようとしています。 今私はすべてのデータを1つの変数に入れており、これはカンザスの郡だけのサブデータを作成しようとしています。私はこれについてどうやって行くのか分からない。R:JSONファイルから特定のものを選択する

答えて

0
library(rjson) 
lst=fromJSON(file = 'http://eric.clst.org/wupl/Stuff/gz_2010_us_040_00_20m.json') 

index = which(sapply(lapply(lst$features,"[[",'properties'),'[[','NAME')=='Kansas') 

subdata = lst$features[[index]] 
1

は、あなたが持っているどのようなあなたにもdata.frameあるsfオブジェクトを与えるために、library(sf)によって直接読み取ることができgeoJson、そこにあります。そして、あなたは、個々の郡をしたいと、あなたは郡のデータを使用する必要があり、通常data.frameのサブセット操作

library(sf) 

sf <- sf::read_sf("http://eric.clst.org/wupl/Stuff/gz_2010_us_040_00_20m.json") 

sf[sf$NAME == "Kansas", ] 

# Simple feature collection with 1 feature and 5 fields 
# geometry type: MULTIPOLYGON 
# dimension:  XY 
# bbox:   xmin: -102.0517 ymin: 36.99308 xmax: -94.58993 ymax: 40.00316 
# epsg (SRID): 4326 
# proj4string: +proj=longlat +datum=WGS84 +no_defs 
#   GEO_ID STATE NAME LSAD CENSUSAREA      geometry 
# 30 0400000US20 20 Kansas  81758.72 MULTIPOLYGON(((-99.541116 3... 

を使用して、見てすることができますJSONのワークフローと一緒に

sf_counties <- sf::read_sf("http://eric.clst.org/wupl/Stuff/gz_2010_us_050_00_500k.json") 
sf_counties[sf_counties$STATE == 20, ] 
+0

カンザス州全体を概説していますが、どのようにしてその国々にさまざまな郡を表示できますか? – Albert

+0

@ColinAlbert [個々の郡](http://eric.clst.org/wupl/Stuff/gz_2010_us_050_00_500k.json)を含むデータセットが必要です。 – SymbolixAU

1

を設定し、することができますjqr

library(jqr) 
url <- 'http://eric.clst.org/wupl/Stuff/gz_2010_us_040_00_20m.json' 
download.file(url, (f <- tempfile(fileext = ".json"))) 
res <- paste0(readLines(f), collapse = " ") 
out <- jq(res, '.features[] | select(.properties.NAME == "Kansas")') 

library(leaflet) 
leaflet() %>% 
    addTiles() %>% 
    addGeoJSON(out) %>% 
    setView(-98, 38, 6) 
のように簡単にマッピングすることができますしてみてください
関連する問題