我正在尝试直接从GitHub读取rds文件。 我可以从git读取任何文件,但是当我尝试使用gzcon读取rds文件时,它要求con的值。
dat <- readRDS(gzcon(url("http://mgimond.github.io/ES218/Data/ABC.rds")))
exception:con尚未定义。 它需要什么类型的连接?
答案 0 :(得分:2)
我发现这可以正常工作,并且比使用临时文件简单得多:
url <- "https://mgimond.github.io/ES218/Data/ACS.rds"
data <- readRDS(url(url, 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软件包还可以通过人口普查API直接访问美国社区调查。例如,注册一个Census API密钥后,以下代码将使您可以从Table 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
答案 1 :(得分:0)
如果遇到问题,一种方法是将文件下载为临时文件。
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
这应该让您靠近!