markdown R Census API数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown R Census API数据相关的知识,希望对你有一定的参考价值。
---
title: "Census"
author: "Tim Kluis"
date: "4/11/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Census
This is an overview of how to utilize TidyCensus for data analysis that requires demographic information. The censusapi tutorial can be found at https://cran.r-project.org/web/packages/censusapi/vignettes/getting-started.html. Additional online documentation can be found at https://www.census.gov/data/developers/data-sets.html.
## Load Required Libraries
```{r libraries}
install.packages("censusapi")
library(tidyverse) # General package for standardize R programming
library(censusapi) # Simplified Census API package
library(sqldf) # Package that allows SQL programming
library(RPostgreSQL) # Package that controls PostgreSQL Database
```
## Identify Census API Key
You need to apply with the Census to obtain an API key to access their online database
```{r apikey, echo=FALSE}
# Add key to .Renviron
Sys.setenv(CENSUS_KEY="e230244144eac2f1fd00198c98b15ecd1afcf6ad")
# Check to see that the expected key is output in your R console
Sys.getenv("CENSUS_KEY")
```
## Gather APIs available from Census
In order to know the APIs that are available to utilize you need to leverage the listCensusApisfunction.
```{r apis}
apis <- listCensusApis()
View(apis)
```
## Gather Varlists from Census
In order to know the variables that are available to utilize you need to leverage the listCensusMetadata function. You need to location the name from the apis dataframe to place in the function to determine the type = variables.
```{r varlists}
sahie_vars <- listCensusMetadata(name = "timeseries/healthins/sahie",
type = "variables")
head(sahie_vars)
```
## Gather Geographies to leveage from Census
In order to know the geography levels that are available to utilize you need to leverage the listCensusMetadata function. You need to locate the geography from the apis dataframe to place in the function to determine the type = geography.
```{r geography}
sahie_geos <- listCensusMetadata(name = "timeseries/healthins/sahie",
type = "geography")
head(sahie_geos)
```
## Gather Data from Census
In order to gather the data from the census you need to leverage the getCensus function. Key variables in the function are name, vars, region, time)
```{r gatherdata}
sahie_states <- getCensus(name = "timeseries/healthins/sahie",
vars = c("NAME", "IPRCAT", "IPR_DESC", "PCTUI_PT"),
region = "state:*",
time = 2016)
head(sahie_states)
```
```{r gatherdata2}
sahie_counties <- getCensus(name = "timeseries/healthins/sahie",
vars = c("NAME", "IPRCAT", "IPR_DESC", "PCTUI_PT"),
region = "county:*",
regionin = "state:01,02",
time = 2017)
head(sahie_counties, n=12L)
```
```{r gatherdata3}
sahie_years <- getCensus(name = "timeseries/healthins/sahie",
vars = c("NAME", "PCTUI_PT"),
region = "state:01",
time = "from 2006 to 2017")
head(sahie_years)
```
以上是关于markdown R Census API数据的主要内容,如果未能解决你的问题,请参考以下文章