如何使用R从美国实况调查机构下载数据?

如何使用R从美国实况调查机构下载数据?,r,census,tigris,R,Census,Tigris,在一个与我的有关的问题中,我想知道如何从下载数据。根据美国实况调查者的说法,链接的http路径非常规则,并且随着时间的推移保持一致。deeplink指南提供了如何访问表格的示例,即: 显示2006-2008年美国社区调查中的表B07010 美国阿拉巴马州和阿拉巴马州奥托加县的3年估算: 00US01001 但我不确定如何将R中的“查看”转换为“下载” 我目前的调查基于以下线索: 当我找到解决方案时,我会更新这篇文章。这是迄今为止我发现的最有效的解决方案: 库(底格里斯) 图书馆(ac

在一个与我的有关的问题中,我想知道如何从下载数据。根据美国实况调查者的说法,链接的http路径非常规则,并且随着时间的推移保持一致。deeplink指南提供了如何访问表格的示例,即:

显示2006-2008年美国社区调查中的表B07010 美国阿拉巴马州和阿拉巴马州奥托加县的3年估算: 00US01001

但我不确定如何将R中的“查看”转换为“下载”

我目前的调查基于以下线索:


  • 当我找到解决方案时,我会更新这篇文章。

    这是迄今为止我发现的最有效的解决方案:

    库(底格里斯)
    图书馆(acs)
    库(stringr)#填充fips代码
    库(gdtools)
    #抓取空间数据(底格里斯)
    #请注意,您可以在tigris包中使用县名称,但不能在acs包的acs.fetch函数中使用县名称,因此我在这里使用FIPS编号。
    #抓取空间数据
    
    国家我不确定这是否有帮助,但是可以通过优秀的
    acs
    软件包访问B07010 2005-2009。
    library(tigris)
    library(acs)
    library(stringr) #to pad fips codes
    library(gdtools)
    
    #grab the spatial data (tigris)
    #note that you can use the county names inthe tigris package but not in the acs.fetch function from the ACS pacakge so I'm using FIPS numbers here.
    #Grab the spatial data
    counties<-c(5,47,61,81,85)
    #solve the 'an error occurred in the secure channel support'
    #firewall issue? #nope. 
    #https://www2.census.gov/geo/tiger/GENZ2015/shp/
    #download via chrome works fine.
    library(gdtools) #did not fix it. 
    #libcurl may fix it
    #https://stackoverflow.com/questions/29688026/vb6-winhhtp-error-occurred-in-the-secure-channel-support
    library(curl)
    tracts<-tracts(state='NY', county = c(5,47,61,81,85), cb=TRUE)
    #It does!
    
    ##----------------get the tabular data--------------------
    #zevross.com/blog
    #get the tabular data
    #in order to do this, you will need an API key from the US Census. 
    
    #Go to https://api.census.gov/data/key_signup.html
    #to request one (takes a minute or two) and then 
    #use the api.key.install function in the `acs` package to use the key.
    
    api.key.install(key="GETYOUROWNKEEY")
    #make a geographic set to grab tabular data (acs)
    geo<-geo.make(state=c("NY"), county = c(5,47,61,81,85), tract = "*")
    #package not updated to 2013 data, so 2012 used as terminal year
    income<-acs.fetch(endyear=2012, span=5, geography=geo, table.number="B19001", col.names ="pretty")
    #pretty gives fully column names, not census abbreviation. 
    #B19001_001 and *.017 are total income and income over $200k
    #what results is not data, but a list of what is available.
    names(attributes(income)) #shows what's available
    attr(income, "acs.colnames")
    
    #convert to data frame for merging. 
    income_df<-data.frame(paste0(
                                str_pad(income@geography$state,2,"left", pad="0"),
                                str_pad(income@geography$county,3,"left", pad = "0"),
                                str_pad(income@geography$tract,6,"left", pad="0")),
                                income@estimate[,c(
                                        "Household Income: Total:",
                                        "Household Income: $200,000 or more")], 
                                        stringsAsFactors=FALSE)
                                        #that worked, 12/18/2017                                                       
    library(dplyr) #required for select
    income_df<-select(income_df, 1:3)
    rownames(income_df)<-1:nrow(income_df)
    names(income_df)<-c("GEOID","total","over_200")
    income_df$percent <-100*(income_df$over_200/income_df$total)
    #works!