Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:从JSON文件中选择某些_Json_R_Geojson - Fatal编程技术网

R:从JSON文件中选择某些

R:从JSON文件中选择某些,json,r,geojson,Json,R,Geojson,我已经从()将一个JSON文件导入到R中,并且我试图只选择堪萨斯州的县。 现在我把所有的数据都放在一个变量中,我正试图把这个变量的子数据放在堪萨斯州的各个县。我不知道该怎么办 您所拥有的是geoJson,可以通过library(sf)直接读取,从而为您提供一个sf对象,该对象也是data.frame。然后您可以使用通常的data.frame子集设置操作 library(rjson) lst=fromJSON(file = 'http://eric.clst.org/wupl/Stuff/gz_

我已经从()将一个JSON文件导入到R中,并且我试图只选择堪萨斯州的县。
现在我把所有的数据都放在一个变量中,我正试图把这个变量的子数据放在堪萨斯州的各个县。我不知道该怎么办

您所拥有的是
geoJson
,可以通过
library(sf)
直接读取,从而为您提供一个
sf
对象,该对象也是
data.frame
。然后您可以使用通常的data.frame子集设置操作

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]]
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...
库(sf)

sf您所拥有的是
geoJson
,可以通过
library(sf)
直接读取,为您提供一个
sf
对象,它也是
data.frame
。然后您可以使用通常的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...
库(sf)

sf要继续使用JSON工作流,可以尝试
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")')

要继续使用JSON工作流,可以尝试
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")')

它概述了整个堪萨斯州,但我如何显示其中的不同县?@ColinAlbert你需要包含的数据集,所以它概述了整个堪萨斯州,但我如何显示其中的不同县?@ColinAlbert你需要包含