walkerke / tidycensus

Load US Census boundary and attribute data as 'tidyverse' and 'sf'-ready data frames in R
https://walker-data.com/tidycensus
Other
639 stars 100 forks source link

no applicable method for 'gather' applied to an object of class "character" #403

Closed srizer closed 3 years ago

srizer commented 3 years ago

I was attempting to follow along with the example in this tweet and got the following error:

> x = get_decennial(geography = "state", variables = "P1_001N", year = 2020)
Getting data from the 2020 decennial Census
Using Census Summary File 1
Error in UseMethod("gather") : 
  no applicable method for 'gather' applied to an object of class "character"

And below is my sessionInfo(). I've updated my version of R, restarted the session, but not sure what my error is. Am I missing some necessary package? Any and all help is appreciated.

R version 4.1.1 (2021-08-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] forcats_0.5.1   stringr_1.4.0   dplyr_1.0.7     purrr_0.3.4     readr_2.0.1     tidyr_1.1.3     tibble_3.1.4    ggplot2_3.3.5   tidyverse_1.3.1 tidycensus_1.0 

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.7         lubridate_1.7.10   lattice_0.20-44    class_7.3-19       assertthat_0.2.1   digest_0.6.27      utf8_1.2.2         cellranger_1.1.0  
 [9] R6_2.5.1           backports_1.2.1    reprex_2.0.1       evaluate_0.14      e1071_1.7-8        httr_1.4.2         pillar_1.6.2       rlang_0.4.11      
[17] curl_4.3.2         readxl_1.3.1       uuid_0.1-4         rstudioapi_0.13    rmarkdown_2.11     rgdal_1.5-27       foreign_0.8-81     munsell_0.5.0     
[25] proxy_0.4-26       broom_0.7.9        modelr_0.1.8       compiler_4.1.1     xfun_0.26          pkgconfig_2.0.3    tigris_1.4.1       htmltools_0.5.2   
[33] tidyselect_1.1.1   fansi_0.5.0        withr_2.4.2        crayon_1.4.1       tzdb_0.1.2         dbplyr_2.1.1       sf_1.0-2           rappdirs_0.3.3    
[41] grid_4.1.1         jsonlite_1.7.2     gtable_0.3.0       lifecycle_1.0.0    DBI_1.1.1          magrittr_2.0.1     units_0.7-2        scales_1.1.1      
[49] KernSmooth_2.23-20 cli_3.0.1          stringi_1.7.4      fs_1.5.0           sp_1.4-5           xml2_1.3.2         ellipsis_0.3.2     generics_0.1.0    
[57] vctrs_0.3.8        tools_4.1.1        glue_1.4.2         hms_1.1.0          fastmap_1.1.0      yaml_2.2.1         colorspace_2.0-2   maptools_1.1-2    
[65] classInt_0.4-3     rvest_1.0.1        haven_2.4.3        knitr_1.34  
donojazz commented 3 years ago

I had that same error yesterday, but downloaded Tidycensud off git. Seems to be working now. I thought Kyle just posted 2020 yesterday. Used this: remotes::install_github("walkerke/tidycensus") library(tidyverse) library(tidycensus) a = get_decennial(geography = "state", variables = "P1_001N", year = 2020) head(a, 5)

Return:

a = get_decennial(geography = "state", variables = "P1_001N", year = 2020) Getting data from the 2020 decennial Census Using the PL 94-171 Redistricting Data summary file head(a, 5)

A tibble: 5 x 4

GEOID NAME variable value

1 01 Alabama P1_001N 5024279 2 02 Alaska P1_001N 733391 3 04 Arizona P1_001N 7151502 4 05 Arkansas P1_001N 3011524 5 06 California P1_001N 39538223
donojazz commented 3 years ago

Just discovered that you must load Tidycensus off Kyle's github or error comes back. I'm not sure how to make it replace my default version.

remotes::install_github("walkerke/tidycensus") library(tidyverse) library(tidycensus) a = get_decennial(geography = "state", variables = "P1_001N", year = 2020) head(a, 5)

srizer commented 3 years ago

Thanks for the workaround!

janicekchen commented 3 years ago

Hi there!

I am encountering the same problem and have uninstalled and reinstalled from the GitHub repository multiple times but I am still getting the error.

race_dec <- tidycensus::get_decennial( geography = "tract", geometry = TRUE, year = 2020, variables = "P1_003N", summary_var = "P1_002N")

walkerke commented 3 years ago

@janicekchen the error is occurring because tract-level data are only available by state, and you haven't specified one. Choose a state, e.g. state = "WA", and it'll work.

janicekchen commented 3 years ago

Wow, my bad. Thank you so much!

kristinchang commented 2 years ago

Hi! I received the same error when trying to retrieve data for the US Virgin Islands. remotes::install_github("walkerke/tidycensus") doesn't seem to resolve my issue:

data <- get_decennial(geography = "tract", variables = c('P010001','P011002'), year = 2010, state= 78, county = 030, geometry = T)

This call works when I change the state and county codes to 42 and 101 respectively for Philly. Any suggestions would be greatly appreciated!

walkerke commented 2 years ago

Hi @kristinchang - the Virgin Islands are found in a different summary file which needs to be stated explicitly. You can read more about it in my book: https://walker-data.com/census-r/an-introduction-to-tidycensus.html#summary-files-in-the-decennial-census.

If you add the argument sumfile = "vi" your code works. Geometry also works but you have to set cb = FALSE as cartographic boundary files are not available for the Virgin Islands in 2010.

get_decennial(
  geography = "tract",
  variables = c('P010001', 'P011002'),
  year = 2010,
  state = 78,
  county = 030,
  geometry = TRUE,
  sumfile = "vi",
  cb = FALSE
)
Simple feature collection with 26 features and 4 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -65.15412 ymin: 18.17959 xmax: -64.76834 ymax: 18.46498
Geodetic CRS:  NAD83
# A tibble: 26 × 5
   GEOID       NAME          variable value                       geometry
   <chr>       <chr>         <chr>    <dbl>             <MULTIPOLYGON [°]>
 1 78030961100 Census Tract… P010001   1214 (((-64.92645 18.33021, -64.92…
 2 78030961100 Census Tract… P011002   1109 (((-64.92645 18.33021, -64.92…
 3 78030960700 Census Tract… P010001    788 (((-64.8127 18.29483, -64.812…
 4 78030960700 Census Tract… P011002    721 (((-64.8127 18.29483, -64.812…
 5 78030960300 Census Tract… P010001   1234 (((-64.89348 18.35058, -64.89…
 6 78030960300 Census Tract… P011002   1106 (((-64.89348 18.35058, -64.89…
 7 78030990000 Census Tract… P010001      0 (((-65.1507 18.30284, -65.144…
 8 78030990000 Census Tract… P011002      0 (((-65.1507 18.30284, -65.144…
 9 78030960200 Census Tract… P010001   1238 (((-64.88142 18.32568, -64.88…
10 78030960200 Census Tract… P011002   1121 (((-64.88142 18.32568, -64.88…
# … with 16 more rows
kristinchang commented 2 years ago

Got it... Thank you so much! @walkerke

hantvu commented 2 years ago

I had that same error yesterday, but downloaded Tidycensud off git. Seems to be working now. I thought Kyle just posted 2020 yesterday. Used this: remotes::install_github("walkerke/tidycensus") library(tidyverse) library(tidycensus) a = get_decennial(geography = "state", variables = "P1_001N", year = 2020) head(a, 5)

Return:

a = get_decennial(geography = "state", variables = "P1_001N", year = 2020) Getting data from the 2020 decennial Census Using the PL 94-171 Redistricting Data summary file head(a, 5)

A tibble: 5 x 4

GEOID NAME variable value 1 01 Alabama P1_001N 5024279 2 02 Alaska P1_001N 733391 3 04 Arizona P1_001N 7151502 4 05 Arkansas P1_001N 3011524 5 06 California P1_001N 39538223

Hello, I got the same error and did what you said by downloaded tidycensus from github, but still got the same error. Anyone can help? I really appreciate.

walkerke commented 2 years ago

Your code should work; could you post the results of your sessionInfo()?

hantvu commented 2 years ago

Your code should work; could you post the results of your sessionInfo()?

Here's the results when I run sessionInfo() R version 4.1.2 (2021-11-01) Platform: aarch64-apple-darwin20 (64-bit) Running under: macOS Monterey 12.2.1

Matrix products: default LAPACK: /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRlapack.dylib

locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages: [1] stats graphics grDevices utils datasets methods base

loaded via a namespace (and not attached): [1] compiler_4.1.2 fastmap_1.1.0 cli_3.2.0 htmltools_0.5.2 [5] tools_4.1.2 yaml_2.2.1 rmarkdown_2.11 knitr_1.37
[9] xfun_0.29 digest_0.6.29 rlang_1.0.2 evaluate_0.14

I'm trying to learn yesterday's workshop material. And I'm new to R. Thanks for your response.

hantvu commented 2 years ago

Your code should work; could you post the results of your sessionInfo()?

By the way, I'm using R Notebook. Should it be the problem? I got this Error when I tried to run your second code (table_p2 <- get_decennial( geography = "state", table = "P2", year = 2020)

Error in UseMethod("left_join") : no applicable method for 'left_join' applied to an object of class "try-error"

munshimdrasel commented 2 years ago

Hy, I'm getting the same error. Can you help me out please?

I've installed tidycensus from github

Here's my session info:

sessionInfo() R version 4.0.2 (2020-06-22) Platform: x86_64-apple-darwin17.0 (64-bit) Running under: OS X Snow Leopard 11.6.5

Matrix products: default LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] forcats_0.5.1 stringr_1.4.0 dplyr_1.0.9 purrr_0.3.4
[5] readr_2.1.2 tidyr_1.2.0 tibble_3.1.7 tidyverse_1.3.1
[9] tidycensus_1.2.1.9000 viridis_0.6.2 viridisLite_0.4.0 ggplot2_3.3.6
[13] sf_1.0-7 data.table_1.14.2

loaded via a namespace (and not attached): [1] Rcpp_1.0.8.3 lubridate_1.8.0 lattice_0.20-45 class_7.3-20 digest_0.6.29
[6] assertthat_0.2.1 utf8_1.2.2 cellranger_1.1.0 R6_2.5.1 backports_1.4.1
[11] reprex_2.0.1 e1071_1.7-9 httr_1.4.3 pillar_1.7.0 rlang_1.0.2
[16] curl_4.3.2 uuid_1.1-0 readxl_1.4.0 rstudioapi_0.13 labeling_0.4.2
[21] rgdal_1.5-32 foreign_0.8-82 munsell_0.5.0 proxy_0.4-26 broom_0.8.0
[26] compiler_4.0.2 modelr_0.1.8 pkgconfig_2.0.3 tigris_1.6 tidyselect_1.1.2
[31] gridExtra_2.3 fansi_1.0.3 crayon_1.5.1 tzdb_0.3.0 dbplyr_2.1.1
[36] withr_2.5.0 rappdirs_0.3.3 grid_4.0.2 jsonlite_1.8.0 gtable_0.3.0
[41] lifecycle_1.0.1 DBI_1.1.2 magrittr_2.0.3 units_0.8-0 scales_1.2.0
[46] KernSmooth_2.23-20 cli_3.3.0 stringi_1.7.6 farver_2.1.0 fs_1.5.2
[51] sp_1.4-7 xml2_1.3.3 ellipsis_0.3.2 generics_0.1.2 vctrs_0.4.1
[56] tools_4.0.2 glue_1.6.2 hms_1.1.1 colorspace_2.0-3 maptools_1.1-4
[61] classInt_0.4-3 rvest_1.0.2 haven_2.5.0

Here's my code: get_decennial(state="TX", year = 2020, geography = "block", variables = 'B19013_001E', geometry = FALSE, show_call = TRUE)

error:

Getting data from the 2020 decennial Census Using FIPS code '48' for state 'TX' Census API call: https://api.census.gov/data/2020/dec/pl?get=B19013_001E%2CNAME&for=block%3A%2A&in=state%3A48%26in%3Dcounty%3A%2A Using the PL 94-171 Redistricting Data summary file Error in UseMethod("gather") : no applicable method for 'gather' applied to an object of class "character"

Thanks Rasel

walkerke commented 2 years ago

@munshimdrasel -

You are trying to get an ACS variable from the decennial Census. Use get_acs() instead - though ACS data only goes down to the block group level.

atrevidoantonio93 commented 1 year ago

I was attempting to follow along with the example given in Spatial data in tidycensus to retrieve census tract level population by race.

I've follow the solutions given above to no avail, with each attempt returning the "Error in UseMethod("gather")" message

Here's my code

racevars <- c(White = "P2_005N", 
              Black = "P2_006N", 
              Asian = "P2_008N", 
              Hispanic = "P2_002N")

king<- get_decennial(
  geography = "tract",
  variables = racevar,
  state = "WA",
  county = "King County",
  geometry = TRUE,
  year = 2020
) 

Here's my session info:

R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22621)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] tidycensus_1.2.5.9000      bea.R_1.0.6                data.table_1.14.4          sf_1.0-8                  
 [5] tigris_1.6.1               seasonal_1.9.0             tsibble_1.1.3              fredr_2.1.0               
 [9] hrbrthemes_0.8.0           ggthemes_4.2.4             ggh4x_0.2.2                ggdist_3.2.0              
[13] distributional_0.3.1       scales_1.2.1               tidyquant_1.0.5            quantmod_0.4.20           
[17] TTR_0.24.3                 PerformanceAnalytics_2.0.4 xts_0.12.2                 zoo_1.8-11                
[21] lubridate_1.9.0            timechange_0.1.1           forcats_0.5.2              stringr_1.4.1             
[25] purrr_0.3.5                readr_2.1.3                tidyr_1.2.1                tibble_3.1.8              
[29] ggplot2_3.4.0              tidyverse_1.3.2            dplyr_1.0.10              

loaded via a namespace (and not attached):
 [1] googledrive_2.0.0    x13binary_1.1.57-3   colorspace_2.0-3     ellipsis_0.3.2       class_7.3-20        
 [6] rprojroot_2.0.3      rgdal_1.5-32         snakecase_0.11.0     fs_1.5.2             rstudioapi_0.14     
[11] proxy_0.4-27         farver_2.1.1         remotes_2.4.2        DT_0.26              fansi_1.0.3         
[16] xml2_1.3.3           codetools_0.2-18     extrafont_0.18       knitr_1.40           jsonlite_1.8.3      
[21] broom_1.0.1          Rttf2pt1_1.3.11      anytime_0.3.9        dbplyr_2.2.1         shinydashboard_0.7.2
[26] shiny_1.7.3          clipr_0.8.0          compiler_4.2.2       httr_1.4.4           googleVis_0.7.0     
[31] backports_1.4.1      assertthat_0.2.1     fastmap_1.1.0        gargle_1.2.1         cli_3.3.0           
[36] s2_1.1.0             later_1.3.0          prettyunits_1.1.1    htmltools_0.5.3      tools_4.2.2         
[41] gtable_0.3.1         glue_1.6.2           wk_0.7.0             rappdirs_0.3.3       Rcpp_1.0.9          
[46] cellranger_1.1.0     vctrs_0.5.0          extrafontdb_1.0      xfun_0.34            ps_1.7.2            
[51] rvest_1.0.3          mime_0.12            lifecycle_1.0.3      googlesheets4_1.0.1  hms_1.1.2           
[56] promises_1.2.0.1     curl_4.3.3           gdtools_0.2.4        stringi_1.7.8        maptools_1.1-5      
[61] e1071_1.7-12         pkgbuild_1.3.1       rlang_1.0.6          pkgconfig_2.0.3      systemfonts_1.0.4   
[66] evaluate_0.18        lattice_0.20-45      htmlwidgets_1.5.4    processx_3.8.0       tidyselect_1.2.0    
[71] magrittr_2.0.3       R6_2.5.1             generics_0.1.3       DBI_1.1.3            pillar_1.8.1        
[76] haven_2.5.1          foreign_0.8-83       withr_2.5.0          units_0.8-0          sp_1.5-1            
[81] janitor_2.1.0        modelr_0.1.9         crayon_1.5.2         uuid_1.1-0           Quandl_2.11.0       
[86] KernSmooth_2.23-20   utf8_1.2.2           tzdb_0.3.0           rmarkdown_2.17       grid_4.2.2          
[91] readxl_1.4.1         callr_3.7.3          reprex_2.0.2         digest_0.6.30        classInt_0.4-8      
[96] xtable_1.8-4         httpuv_1.6.6         munsell_0.5.0        quadprog_1.5-8 

Any help would be much appreciated!

walkerke commented 1 year ago

@atrevidoantonio93 you have a typo in your code. You are storing the variable names in racevars but requesting variables using racevar. Fixing this should get your code to run.

atrevidoantonio93 commented 1 year ago

@walkerke I fixed the typo but still receive the original error.

walkerke commented 1 year ago

@atrevidoantonio93 not sure then, your code runs fine on my end. Is your internet connection OK? Can you do this successfully from R?

req <- httr::GET("https://api.census.gov/data/2020/dec/pl?get=P2_005N%2CP2_006N%2CP2_008N%2CP2_002N%2CNAME&for=tract%3A%2A&in=state%3A53%2Bcounty%3A033")

req$status_code # Should be 200
atrevidoantonio93 commented 1 year ago

Yes, that works fine.

hanifsajid commented 1 year ago

I had the same issue. Actually, my Census Data API Key was not activated. Make sure to click on the link in the email to activate your API key.

willshelley404 commented 1 year ago

Getting the same error: Getting data from the 2020 decennial Census Using FIPS code '53' for state 'WA' No encoding supplied: defaulting to UTF-8. Using the PL 94-171 Redistricting Data Summary File Error in UseMethod("gather") : no applicable method for 'gather' applied to an object of class "character"

I have the latest version from github and am able to run this code sucessfully:

req <- httr::GET("https://api.census.gov/data/2020/dec/pl?get=P2_005N%2CP2_006N%2CP2_008N%2CP2_002N%2CNAME&for=tract%3A%2A&in=state%3A53%2Bcounty%3A033")

Trying this code: get_decennial( geography = "state", state = 'TN', variables = "P1_001N", year = 2020 )

Here is my session info:

sessionInfo() R version 4.2.2 (2022-10-31 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale: [1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C
[5] LC_TIME=English_United States.utf8

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] lubridate_1.9.2 forcats_1.0.0 stringr_1.5.0 dplyr_1.1.2 purrr_1.0.2 readr_2.1.4
[7] tidyr_1.3.0 tibble_3.2.1 ggplot2_3.4.2 tidyverse_2.0.0 tidycensus_1.4.4

loaded via a namespace (and not attached): [1] tidyselect_1.2.0 xfun_0.40 remotes_2.4.2.1 sf_1.0-14 colorspace_2.1-0
[6] vctrs_0.6.3 generics_0.1.3 htmltools_0.5.6 yaml_2.3.7 utf8_1.2.3
[11] rlang_1.1.1 e1071_1.7-13 pillar_1.9.0 withr_2.5.0 glue_1.6.2
[16] DBI_1.1.3 rappdirs_0.3.3 uuid_1.1-0 lifecycle_1.0.3 munsell_0.5.0
[21] gtable_0.3.3 rvest_1.0.3 tigris_2.0.3 evaluate_0.21 knitr_1.43
[26] tzdb_0.4.0 fastmap_1.1.1 curl_5.0.1 class_7.3-20 fansi_1.0.4
[31] Rcpp_1.0.11 KernSmooth_2.23-20 scales_1.2.1 classInt_0.4-9 jsonlite_1.8.7
[36] hms_1.1.3 digest_0.6.33 stringi_1.7.12 grid_4.2.2 cli_3.6.1
[41] tools_4.2.2 magrittr_2.0.3 proxy_0.4-27 crayon_1.5.2 pkgconfig_2.0.3
[46] xml2_1.3.5 timechange_0.2.0 rmarkdown_2.23 httr_1.4.6 rstudioapi_0.15.0 [51] R6_2.5.1 units_0.8-3 compiler_4.2.2

walkerke commented 1 year ago

@willshelley404 unfortunately, I can't reproduce your error. Typically this is can be fixed with a package update, so I'm not sure what is going on here.