Comments (4)
Right now this isn't supported. Would you mind explaining the use case a bit more? Would you need data for tracts that are potentially scattered around the US?
Tract-specific data are currently available from the API if a state and county are specified. Getting data for specific tracts at a time isn't currently available as I haven't had a request for it, but you could conceivably write a wrapper function for this. For example:
library(tidycensus)
library(tidyverse)
tract_ids <- c("36103158710", "12103027704", "47145030202", "37119005914",
"24003731201", "24017851400", "47105060301", "42045401200",
"39153502700", "55079120202")
get_tracts <- function(tracts, variables) {
map_df(tracts, function(x) {
state <- str_sub(x, 1, 2)
county <- str_sub(x, 3, 5)
get_decennial(geography = "tract",
variables = variables,
state = state,
county = county) %>%
filter(GEOID == x)
})
}
get_tracts(tract_ids, "P0010001")
# A tibble: 10 x 4
GEOID NAME variable value
<chr> <chr> <chr> <dbl>
1 36103158710 Census Tract 1587.10 P0010001 3686.
2 12103027704 Census Tract 277.04 P0010001 2423.
3 47145030202 Census Tract 302.02 P0010001 7319.
4 37119005914 Census Tract 59.14 P0010001 5563.
5 24003731201 Census Tract 7312.01 P0010001 6354.
6 24017851400 Census Tract 8514 P0010001 8176.
7 47105060301 Census Tract 603.01 P0010001 3332.
8 42045401200 Census Tract 4012 P0010001 3636.
9 39153502700 Census Tract 5027 P0010001 6574.
10 55079120202 Census Tract 1202.02 P0010001 3173.
On my machine, this runs quickly:
> system.time(get_tracts(tract_ids, "P0010001"))
user system elapsed
0.37 0.00 1.75
Given the structure of the Census API, I'm not sure the time savings would be substantial as tidycensus would need to do something similar under the hood to stitch together different API calls.
from tidycensus.
from tidycensus.
Thanks for the reply! Yes I think that is the best move. We can check what this would look like by using the censusapi package, which allows for the acquisition of specific tract data. tidycensus would need to implement something like this under the hood.
library(censusapi)
Sys.setenv(CENSUS_KEY = Sys.getenv("CENSUS_API_KEY"))
get_tracts2 <- function(tracts, variables) {
map_df(tracts, function(x) {
getCensus(name = "sf1",
vintage = 2010,
vars = variables,
region = paste0("tract:",
str_sub(x, 6, 11)),
regionin = paste0("state:",
str_sub(x, 1, 2),
"+county:",
str_sub(x, 3, 5))
)
})
}
> get_tracts2(tract_ids, "P0010001")
state county tract P0010001
1 36 103 158710 3686
2 12 103 027704 2423
3 47 145 030202 7319
4 37 119 005914 5563
5 24 003 731201 6354
6 24 017 851400 8176
7 47 105 060301 3332
8 42 045 401200 3636
9 39 153 502700 6574
10 55 079 120202 3173
This runs a bit slower than the earlier code sample from tidycensus:
> system.time(get_tracts2(tract_ids, "P0010001"))
user system elapsed
0.30 0.03 2.31
from tidycensus.
Thanks for this tip. I am one of the contributors to the censusr
package, and this is a key feature for the development of the package.
My colleague and I run travel models on metropolitan regions all over the US. A region is usually defined by a set of counties, sometimes in one state and sometimes not.
We are considering deprecating that package and sending users over here so we can consolidate efforts.
from tidycensus.
Related Issues (20)
- get_pums and/or Census API issue HOT 4
- Add documentation to load_variables() about datasets
- geography column empty for load_variables() for 2022 ACS 5-year estimate variables HOT 1
- return_vacant and 'VACS' variable not working for get_pums HOT 4
- Failed with error: ‘lazy-load database 'C:/R/win-library/4.0/tidycensus/data/Rdata.rdb' is corrupt’ HOT 2
- rate_sleep() error HOT 2
- `tidycensus::get_acs()` does not have the limitations that `tigris::zctas()` does for specifying `state` for a given `year`
- Pulling from multiple source tables with `get_acs()` causes erroneous NAs HOT 2
- Feature request to make "Margin of Error" optional in get_acs(). HOT 1
- get_decennial gives incorrect values for 2000 aian and aianprofile
- Connecticut data absent in county-level geography request HOT 2
- CBSA and CSA shape files do not exists for 2022 -- Need an error message staying that
- Does `interpolate_pw` use population-weighted areal interpolation? HOT 1
- Issue with accessing table B05001 at census tract level HOT 2
- get_acs for 2022 at the congressional district tries to use wrong layer HOT 2
- DP04 Missing DP04_0005P Variable HOT 1
- Error in get_decennial: API returning unknown variable error for variable returned from call to load_variables HOT 1
- decennial 1990 not available? HOT 1
- get_decennial(): GEOCOMP: inconsistent behavior
- get_estimates show_call is not displaying HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tidycensus.