GithubHelp home page GithubHelp logo

Comments (4)

walkerke avatar walkerke commented on August 18, 2024

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.

vinhdizzo avatar vinhdizzo commented on August 18, 2024

from tidycensus.

walkerke avatar walkerke commented on August 18, 2024

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.

josiekre avatar josiekre commented on August 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.