簡體   English   中英

從 git 讀取 Rds 文件

[英]Reading Rds file from git

我正在嘗試直接從 GitHub 讀取 rds 文件。 我可以從 git 讀取任何文件,但是當我嘗試使用 gzcon 讀取 rds 文件時,它的詢問值是 con。

dat <- readRDS(gzcon(url("http://mgimond.github.io/ES218/Data/ABC.rds")))

例外: con 尚未定義。 它需要什么類型的連接?

我發現這比使用臨時文件更有效並且簡單得多:

u <- "https://mgimond.github.io/ES218/Data/ACS.rds"
data <- readRDS(url(u, method="libcurl"))

您可以看到這已正確讀取數據,因為:

> names(data)
 [1] "County"     "State"      "B19013001"  "B19013001s" "B23006001" 
 [6] "B23006002"  "B23006003"  "B23006004"  "B23006005"  "B23006006" 
[11] "B23006007"  "B23006008"  "B23006009"  "B23006010"  "B23006011" 
[16] "B23006012"  "B23006013"  "B23006014"  "B23006015"  "B23006016" 
[21] "B23006017"  "B23006018"  "B23006019"  "B23006020"  "B23006021" 
[26] "B23006022"  "B23006023"  "B23006024"  "B23006025"  "B23006026" 
[31] "B23006027"  "B23006028"  "B23006029"  "B23006001s" "B23006002s"
[36] "B23006003s" "B23006004s" "B23006005s" "B23006006s" "B23006007s"
[41] "B23006008s" "B23006009s" "B23006010s" "B23006011s" "B23006012s"
[46] "B23006013s" "B23006014s" "B23006015s" "B23006016s" "B23006017s"
[51] "B23006018s" "B23006019s" "B23006020s" "B23006021s" "B23006022s"
[56] "B23006023s" "B23006024s" "B23006025s" "B23006026s" "B23006027s"
[61] "B23006028s" "B23006029s"

我確實看到這似乎是美國社區調查。 人口普查有一個 API,允許您直接通過他們的 API 訪問這些數據。 您可能想在這里探索: https ://www.census.gov/data/developers/data-sets.html。

R中的tidycensus包還可以通過 Census API 直接訪問美國社區調查。 例如,在注冊人口普查 API 密鑰后,以下代碼將允許您訪問表 DP05中的 2017 ACS-1 數據:

library(readr)
library(tidyverse)
library(tidycensus)

#Get census API key at:  https://api.census.gov/data/key_signup.html
census_api_key("insert your API key here")

#Get Age Data from 2017 ACS-1 Survey; includes DC and Puerto Rico
DP052017 <- get_acs(geography = "state", year = 2017, table = "DP05", 
                    cache_table = TRUE, survey = "acs1")

head(DP052017)

head(DP052017)
# A tibble: 6 x 5
  GEOID NAME    variable    estimate    moe
  <chr> <chr>   <chr>          <dbl>  <dbl>
1 01    Alabama DP05_0001  4874747     NA  
2 01    Alabama DP05_0001P 4874747     NA  
3 01    Alabama DP05_0002  2359896   5397  
4 01    Alabama DP05_0002P      48.4    0.1
5 01    Alabama DP05_0003  2514851   5397  
6 01    Alabama DP05_0003P      51.6    0.1

如果您遇到問題,一種方法是將文件下載為臨時文件。

url <- "mgimond.github.io/ES218/Data/ACS.rds"
temp <- tempfile() # create a tempfile
download.file(url, temp) # download to disk
dat <- readRDS(temp) # read the tempfile
unlink(temp) # Deletes tempfile

這應該讓你接近!

暫無
暫無

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

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