2017-07-14 7 views
3

私はtidycensusパッケージを使用して、米国のすべてのブロックグループから国勢調査データをダウンロードするプロセスを自動化する予定です。 US内のすべての領域をダウンロードするように開発者から指示がありますが、同じmethodを使用してブロックグループにアクセスすることはできません。ここ r tidycensusすべてのブロックグループをダウンロード

は、結果として得られるエラーも

動作しない

No encoding supplied: defaulting to UTF-8. 
Error: parse error: premature EOF 

        (right here) ------^ 

リストに各状態内の郡を変換するための類似のアプローチである

library(tidyverse) 
library(tidycensus) 
census_api_key("key here") 

# create lists of state and county codes 

data("fips_codes") 
temp <- data.frame(state = as.character(fips_codes$state_code), 
        county = fips_codes$county_code, 
        stringsAsFactors = F) 
temp <- aggregate(county~state, temp, c) 
state <- temp$state 
coun <- temp$county 

# use map2_df to loop through the files, similar to the "tract" data pull 

home <- map2_df(state, coun, function(x,y) { 
get_acs(geography = "block group", variables = "B25038_001", #random var 
state = x,county = y) 
    }) 

動作しない私の現在のコードであります

temp <- aggregate(county~state, temp, c) 
state <- temp$state 
coun <- temp$county 

df<- map2_df(state, coun, function(x,y) { 
    get_acs(geography = "block group", variables = "B25038_001", 
      state = x,county = y) 
    }) 

Error: Result 1 is not a length 1 atomic vectorが返されます。

これをどのように完了できるか理解している人はいますか?おそらく私は関数や構文を正しく使用していない可能性がありますが、私もループはあまりよくありません。どんな助けもありがとう。

答えて

1

totalcensushttps://github.com/GL-Li/totalcensus)このパッケージをお試しください。国勢調査データファイルを自分のコンピュータにダウンロードし、これらのファイルからデータを抽出します。フォルダとパスを設定した後、2015年のACS 5年間の調査ですべてのブロックグループデータが必要な場合は、以下のコードを実行します。すべての州とDCの217739のブロックグループの

library(totalcensus) 

# download the 2015 ACS 5-year survey data, which is about 50 GB. 
download_census("acs5year", 2015) 

# read block group data of variable B25038_001 from all states plus DC 
block_groups <- read_acs5year(
    year = 2015, 
    states = states_DC, 
    table_contents = "B25038_001", 
    summary_level = "block group" 
) 

抽出したデータ:

#      GEOID  lon  lat state population B25038_001 GEOCOMP SUMLEV                NAME 
    #  1: 15000US020130001001 -164.1232 54.80448 AK  982   91  all 150  Block Group 1, Census Tract 1, Aleutians East Borough, Alaska 
    #  2: 15000US020130001002 -161.1786 55.60224 AK  1116  247  all 150  Block Group 2, Census Tract 1, Aleutians East Borough, Alaska 
    #  3: 15000US020130001003 -160.0655 55.13399 AK  1206  352  all 150  Block Group 3, Census Tract 1, Aleutians East Borough, Alaska 
    #  4: 15000US020160001001 178.3388 51.95945 AK  1065  264  all 150 Block Group 1, Census Tract 1, Aleutians West Census Area, Alaska 
    #  5: 15000US020160002001 -166.8899 53.85881 AK  2038  380  all 150 Block Group 1, Census Tract 2, Aleutians West Census Area, Alaska 
    # ---                                      
    # 217735: 15000US560459511001 -104.7889 43.99520 WY  1392  651  all 150   Block Group 1, Census Tract 9511, Weston County, Wyoming 
    # 217736: 15000US560459511002 -104.4785 43.76853 WY  2050  742  all 150   Block Group 2, Census Tract 9511, Weston County, Wyoming 
    # 217737: 15000US560459513001 -104.2575 43.88160 WY  1291  520  all 150   Block Group 1, Census Tract 9513, Weston County, Wyoming 
    # 217738: 15000US560459513002 -104.1807 43.85406 WY  1046  526  all 150   Block Group 2, Census Tract 9513, Weston County, Wyoming 
    # 217739: 15000US560459513003 -104.2601 43.84355 WY  1373  547  all 150   Block Group 3, Census Tract 9513, Weston County, Wyoming 
+0

センサス2000はまだカバーしていますか? – Mox

+0

@Moxはまだ、来月に追加されます –

+0

これに関するアップデートはありますか? – Mox

3

溶液はtidycensusの著者(カイル・ウォーカー)によって提供され、次のようになりました。

残念ながら、これはただ現時点では動作しません。それが機能した場合、 コードでは、で評価された関数 内の各州内の郡を特定し、次にデータセット と州ごとにステッチを合わせる必要があります。問題は、ブロックグループ のデータは郡のみが利用できるため、米国内のすべての郡( )を順番に歩く必要があることです。それは仕事をした場合は、呼び出しが成功 は次のようになります。

library(tigris) 
library(tidyverse) 
library(tidycensus) 
library(sf) 

ctys <- counties(cb = TRUE) 

state_codes <- unique(fips_codes$state_code)[1:51] 

bgs <- map_df(state_codes, function(state_code) { 
    state <- filter(ctys, STATEFP == state_code) 
    county_codes <- state$COUNTYFP 
    get_acs(geography = "block group", variables = "B25038_001", 
      state = state_code, county = county_codes) 
}) 

問題であることを、私は多状態のために内 通話、またはマルチ郡の通話を可能にするために、内部ロジックを持ちながら、州、tidycensusはまだ 複数の州と複数の郡の呼び出しを同時に処理することはできません。

+0

私は質問者だけでデータを望んでいたと思うしながら、コードのこのビットは、実際に、シェープファイルを取得します。 – Mox

関連する問題