Closed mcgaugj closed 5 years ago
Looks like the issue is that you're trying to get tract-level data from the 1-year ACS estimates, but this data is only available as a 5-year estimate. You can look at this table from the Census Bureau (admittedly this is very poorly formatted) to see what geographies are available as 1- or 5-year estimates.
Generally, the ACS provides 1-year estimates for areas with more than 65,000 people -- so this could be counties, congressional districts, PUMAs, etc. But for tracts, block groups, and other small area geographies, you have to use the 5-year estimates.
See below for an example of pulling tract-level estimates from the 5-year ACS:
library(tidycensus)
ACS1 <- get_acs(
geography = "tract",
survey = "acs5",
table = "B19001",
state = "CA",
county = "Alameda",
geometry = TRUE
)
#> Getting data from the 2013-2017 5-year ACS
head(ACS1)
#> Simple feature collection with 6 features and 5 fields
#> geometry type: MULTIPOLYGON
#> dimension: XY
#> bbox: xmin: -122.2469 ymin: 37.84996 xmax: -122.2124 ymax: 37.88544
#> epsg (SRID): 4269
#> proj4string: +proj=longlat +datum=NAD83 +no_defs
#> GEOID NAME variable
#> 1 06001400100 Census Tract 4001, Alameda County, California B19001_001
#> 2 06001400100 Census Tract 4001, Alameda County, California B19001_002
#> 3 06001400100 Census Tract 4001, Alameda County, California B19001_003
#> 4 06001400100 Census Tract 4001, Alameda County, California B19001_004
#> 5 06001400100 Census Tract 4001, Alameda County, California B19001_005
#> 6 06001400100 Census Tract 4001, Alameda County, California B19001_006
#> estimate moe geometry
#> 1 1254 65 MULTIPOLYGON (((-122.2469 3...
#> 2 24 24 MULTIPOLYGON (((-122.2469 3...
#> 3 24 22 MULTIPOLYGON (((-122.2469 3...
#> 4 8 13 MULTIPOLYGON (((-122.2469 3...
#> 5 14 16 MULTIPOLYGON (((-122.2469 3...
#> 6 52 45 MULTIPOLYGON (((-122.2469 3...
Created on 2019-02-01 by the reprex package (v0.2.1)
1) first of all, it is tidyverse .. you missed the "e" library(tidyverse)
2) this variable does not exist in the ACS1 survey. But ACS5 it works
3) what you are getting ACS2 is just the library list ... what is fine, it supposedly works
Thanks both. I fixed the typo in the package and then switched to the acs5, but it's throwing errors in both (I know the second one only pulls the variables). Both say there are invalid characters in the JSON. Here's the errors:
ACS1 <- get_acs(geography = "tract", survey = "acs5", table = "B19001", state = "CA", county = "Alameda", geometry = TRUE) Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set
options(tigris_use_cache = TRUE)
. Loading ACS5 variables for 2015 from table B19001. To cache this dataset for faster access to ACS tables in the future, run this function withcache_table = TRUE
. You only need to do this once per ACS dataset. Error: lexical error: invalid char in json text.Error report (right here) ------^ ACS2 <- load_variables(2017, "acs5", cache = TRUE) Error: lexical error: invalid char in json text. Error report (right here) ------^
Hmmm. It seems like you're having trouble parsing the JSON that is returned from the Census API. You could try updating your version of jsonlite
-- I have the most recent CRAN version (1.6) and everything appears to be working fine on my end. Also, can you paste the results of devtools::session_info()
or sessionInfo()
install.packages(jsonlite)
yep, would say the same
considering this: https://github.com/walkerke/tidycensus/issues/117
I think you could be using some old package stuff
@mcgaugj did you activate your API key? That can sometimes trigger an error like this. If not, go back to the email from Census where you received your key and click the link in the email to activate it.
Otherwise, make sure that you are running the latest version of tidycensus from CRAN - this will be version 0.9.
It was the jsonlite package. I was on an old version. I'm in business gentleman! Thank you very much for your help. It is much appreciated.
Glad you got it working, @mcgaugj! Do you happen to know which version of jsonlite you had installed before you updated?
@walkerke, perhaps we should specify a minimum required version of jsonline in imports?
It was 1.5 (one version made a difference).
Hmmm...I just installed jsonlite 1.5 on my machine and I'm not getting the parsing error you got, @mcgaugj . Anyhow, glad it's working for you now!
Hi There. I'm a bit of a novice so please forgive me if this is super basic. I have tried troubleshooting on my own and this is my last resort. I'm simply trying to get some data out of the API and I don't even get that far. Here is what I am running:
library(tidyvers) library(tidycensus) library(sf)
census_api_key("XXXXXXXXXXXXXXXXXXXXXXXXXXXX", install = TRUE)
readRenviron("~/.Renviron")
Sys.getenv("CENSUS_API_KEY")
ACS1 <- get_acs(geography = "tract", survey = "acs1", table = "B19001", state = "CA", county = "Alameda", geometry = TRUE)
ACS2 <- load_variables(2017, "acs5", cache = TRUE)
And here is the error messages:
Do you have any idea what I'm doing wrong?