Movatterモバイル変換


[0]ホーム

URL:


censusr

Build Status

We have decided to deprecate censusr and instead use (and contribute)totidycensus.Censusr will remain on CRAN, but support will discontinue.

Access data hosted by the US Census Bureau.

The Census has made a very nice API for data scientists to accesstheir data tables. Thecensusr package will help R usersaccess this API in a convenient and R-like way.

The API works by sending a specially-formatted URL to the the CensusAPI server, which returns an XML or JSON document containing therequested information. In practice, any table available on AmericanFactFinder is available through the API, though the user will need tofind the raw name for the variable in the Census APIguide.

Setup

These instructions are modified from hadley’sAPIbest practices documentation.

  1. Users of this package will need to request an API key, which isavailable for free from the Census Bureau on request. Go tohttp://api.census.gov/data/key_signup.htmlto register. Copy this token to your clipboard.

  2. Identify your home directory. If you are not sure what it is,enternormalizePath("~/") in an R session. If in RStudio,use the R console.

  3. Create a new text file. If in RStudio, do File > New File >Text file.

  4. Create a line like this:

CENSUS_TOKEN=blahblahblahblahblahblah

where the nameCENSUS_TOKEN reminds you which API thisis for andblahblahblahblahblahblah is your token, pastedfrom the clipboard. Make sure the last line in the file is empty. (If itis not empty, R will silently fail to load the file. If you’re using aneditor that shows line numbers, there should be two lines, where thesecond one is empty.)

  1. Save this file in your home directory with the filename.Renviron. If questioned, YES you do want to use a filenamethat begins with a dot..

Note that by default dotfiles are usually hidden. But within RStudio,the file browser will make.Renviron visible and thereforeeasy to edit in the future.

  1. Restart R..Renviron is processed only at the startof an R session.

  2. UseSys.getenv() to access your token. Forexample,

call_census_api(...,api_key =Sys.getenv("CENSUS_TOKEN") ...)

FAQ: Why define this environment variable via.Renvironinstead of in.bash_profile or.bashrc?

Because there are many combinations of OS and ways of running R wherethe.Renviron approach “just works”” and the bash stuffdoes not. When R is a child process of, say, Emacs or RStudio, you can’talways count on environment variables being passed to R. Put them in anR-specific start-up file and save yourself some grief.

Use

The package works by sending a list of requested variables and a listof geographies. The call below requests the number of households owning0, 1, 2, 3, or 4 or more vehicles in Wake County, North Carolina(geoid = 37183). We specify that we want this table for2012 5-year summary level.

library(censusr)call_census_api(paste("B08201_",sprintf("%03d",2:6),"E",sep =""),names =c(0:4),geoids ="37183",data_source ="acs",year =2012,period =5)
## Source: local data frame [1 x 6]## ##   geoid     0      1      2     3     4##   (chr) (dbl)  (dbl)  (dbl) (dbl) (dbl)## 1 37183 15813 111992 149742 47222 16534

We can use theallgeos argument to say that we actuallywant these variables forall census tracts within WakeCounty.

est<-call_census_api(paste("B08201_",sprintf("%03d",2:6),"E",sep =""),names =paste0("est_",c(0:4)),geoids ="37183",allgeos ="tr",data_source ="acs",year =2012,period =5)est
## Source: local data frame [187 x 6]## ##          geoid est_0 est_1 est_2 est_3 est_4##          (chr) (dbl) (dbl) (dbl) (dbl) (dbl)## 1  37183050100   248   516   310    37     0## 2  37183050300   293   826   489    51    19## 3  37183050400    44   369   328    23     9## 4  37183050500   181   885   436    87    30## 5  37183050600   289   600   209    69    19## 6  37183050700   503   584   218   118     0## 7  37183050800   359   227   162    74     0## 8  37183050900   442   249    80     3     0## 9  37183051000   202   543   329    68    28## 10 37183051101   149   201   208    54    64## ..         ...   ...   ...   ...   ...   ...

If we want the margins of error on this table instead of theestimates, we can change the variable to call theM typeinstead of theE type.

moe<-call_census_api(paste("B08201_",sprintf("%03d",2:6),"M",sep =""),names =paste0("moe_",c(0:4)),geoids ="37183",allgeos ="tr",data_source ="acs",year =2012,period =5)moe
## Source: local data frame [187 x 6]## ##          geoid moe_0 moe_1 moe_2 moe_3 moe_4##          (chr) (dbl) (dbl) (dbl) (dbl) (dbl)## 1  37183050100    87   169    81    52    13## 2  37183050300   101   163   106    53    22## 3  37183050400    25    80    62    21    13## 4  37183050500    75   143    98    54    29## 5  37183050600    95   112    76    47    14## 6  37183050700   109    97    82    61    13## 7  37183050800    94    85    73    58    13## 8  37183050900    91    78    43     6    13## 9  37183051000    96   117    99    61    44## 10 37183051101    78    75    80    83    79## ..         ...   ...   ...   ...   ...   ...

[8]ページ先頭

©2009-2025 Movatter.jp