簡體   English   中英

JSON到R不讀取列名

[英]JSON to R not reading column names

我正在嘗試從JSON附帶的美國人口普查網站中提取一些數據。 看起來是這樣的:

data_from_api <- readr::read_file('https://api.census.gov/data/2016/zbp?get=ESTAB,EMPSZES,EMPSZES_TTL,ST,YEAR&for=ZIPCODE:20004')

data_from_api

嘗試使用jsonlite看起來像這樣

> data_from_api <- fromJSON(data_from_api)
> data_from_api
      [,1]    [,2]      [,3]                                          [,4] [,5]   [,6]     
 [1,] "ESTAB" "EMPSZES" "EMPSZES_TTL"                                 "ST" "YEAR" "zipcode"
 [2,] "925"   "001"     "All establishments"                          "11" "2016" "20004"  
 [3,] "406"   "212"     "Establishments with 1 to 4 employees"        "11" "2016" "20004"  
 [4,] "154"   "220"     "Establishments with 5 to 9 employees"        "11" "2016" "20004"  
 [5,] "113"   "230"     "Establishments with 10 to 19 employees"      "11" "2016" "20004"  
 [6,] "122"   "241"     "Establishments with 20 to 49 employees"      "11" "2016" "20004"  
 [7,] "70"    "242"     "Establishments with 50 to 99 employees"      "11" "2016" "20004"  
 [8,] "45"    "251"     "Establishments with 100 to 249 employees"    "11" "2016" "20004"  
 [9,] "8"     "252"     "Establishments with 250 to 499 employees"    "11" "2016" "20004"  
[10,] "6"     "254"     "Establishments with 500 to 999 employees"    "11" "2016" "20004"  
[11,] "1"     "260"     "Establishments with 1,000 employees or more" "11" "2016" "20004" 

知道為什么列名不能正確流動嗎? 我可以更改任何輸入使其工作嗎?

謝謝

這不是由於fromJSON的某些問題,而僅僅是JSON結構的隨機性。

將其轉換為正確命名的data.frame很簡單:

colnms <- data_from_api[1,]
data_from_api <- as.data.frame(data_from_api[-1,], check.names = F, stringsAsFactors = FALSE)
names(data_from_api) <- colnms

它以列表列表(即矩陣)而不是字典(框架)的形式提供。 要獲取框架,請執行一些簡單的操作:

x <- jsonlite::fromJSON(data_from_api)
x
#       [,1]    [,2]      [,3]                                          [,4] [,5]   [,6]     
#  [1,] "ESTAB" "EMPSZES" "EMPSZES_TTL"                                 "ST" "YEAR" "zipcode"
#  [2,] "925"   "001"     "All establishments"                          "11" "2016" "20004"  
#  [3,] "406"   "212"     "Establishments with 1 to 4 employees"        "11" "2016" "20004"  
#  [4,] "154"   "220"     "Establishments with 5 to 9 employees"        "11" "2016" "20004"  
#  [5,] "113"   "230"     "Establishments with 10 to 19 employees"      "11" "2016" "20004"  
#  [6,] "122"   "241"     "Establishments with 20 to 49 employees"      "11" "2016" "20004"  
#  [7,] "70"    "242"     "Establishments with 50 to 99 employees"      "11" "2016" "20004"  
#  [8,] "45"    "251"     "Establishments with 100 to 249 employees"    "11" "2016" "20004"  
#  [9,] "8"     "252"     "Establishments with 250 to 499 employees"    "11" "2016" "20004"  
# [10,] "6"     "254"     "Establishments with 500 to 999 employees"    "11" "2016" "20004"  
# [11,] "1"     "260"     "Establishments with 1,000 employees or more" "11" "2016" "20004"  


colnames(x) <- x[1,]
x <- x[-1,]
x2 <- as.data.frame(x, stringsAsFactors = FALSE)
x2[c(1,2,4,5,6)] <- lapply(x2[c(1,2,4,5,6)], as.integer)

str(x2)
# 'data.frame': 10 obs. of  6 variables:
#  $ ESTAB      : int  925 406 154 113 122 70 45 8 6 1
#  $ EMPSZES    : int  1 212 220 230 241 242 251 252 254 260
#  $ EMPSZES_TTL: chr  "All establishments" "Establishments with 1 to 4 employees" "Establishments with 5 to 9 employees" "Establishments with 10 to 19 employees" ...
#  $ ST         : int  11 11 11 11 11 11 11 11 11 11
#  $ YEAR       : int  2016 2016 2016 2016 2016 2016 2016 2016 2016 2016
#  $ zipcode    : int  20004 20004 20004 20004 20004 20004 20004 20004 20004 20004

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM