对“地点”地理位置使用Tidycensus和map_dfr时出错

fv2wmkja  于 2023-05-20  发布在  其他
关注(0)|答案(1)|浏览(105)

当使用TidyCensus提取ACS数据时,我使用map_dfr循环查看“地点”地理位置的多年数据。我使用的模板是herehere
我收到以下错误,我坚持调试它。希望你能帮我搞清楚到底发生了什么。谢谢。

library(tidycensus, tidyverse, purrr)

#list years
years <- lst(2011:2020) 
names(years) <- years

# census variables
my_vars <- c(
   total_pop = "B01003_001",
   pop_poverty = "B17001_001"
)

# loop over list of years and get 1 year acs estimates
multi_year <- map_dfr(
   years,
   ~ {get_acs(
      geography = "place",
      variables = my_vars,
      year = .x,
      survey = "acs1",
      geometry = FALSE
   )  
   },
   .id = "year"  # when combining results, add id var (name of list item)
) %>%
   filter(GEOID = 0644000) %>% 
   select(-moe) %>%  
   arrange(variable, NAME) %>% 
   print()

Getting data from the 2011 1-year ACSGetting data from the 2012 1-year ACSGetting data from the 2013 1-year ACSGetting data from the 2014 1-year ACSGetting data from the 2015 1-year ACSGetting data from the 2016 1-year ACSGetting data from the 2017 1-year ACSGetting data from the 2018 1-year ACSGetting data from the 2019 1-year ACSGetting data from the 2020 1-year ACS
The 1-year ACS provides data for geographies with populations of 65,000 and greater.
Error in `map()`:
ℹ In index: 1.
ℹ With name: 2011:2020.
Caused by error in `parse_url()`:
! length(url) == 1 is not TRUE
Run `rlang::last_trace()` to see where the error occurred.
Warning messages:
1: In if (year < 2005) { :
  the condition has length > 1 and only the first element will be used
2: In if (year < 2013) { :
  the condition has length > 1 and only the first element will be used
3: In if (year < 2013) { :
  the condition has length > 1 and only the first element will be used
8xiog9wr

8xiog9wr1#

问题是使用dplyr::lst(2011:2012)创建了一个只有一个元素的list,即a vector年:

years <- lst(2011:2012)
names(years) <- years

years
#> $`2011:2012`
#> [1] 2011 2012

因此,不是在years上循环,而是向get_acs传递vector年。
相反,使用命名的vector来实现所需的结果:

library(tidycensus)
library(dplyr, warn = FALSE)
library(purrr)

years <- c(2011:2012)
names(years) <- years

years
#> 2011 2012 
#> 2011 2012

my_vars <- c(
  total_pop = "B01003_001",
  pop_poverty = "B17001_001"
)

multi_year <- map_dfr(
  years,
  ~ {
    get_acs(
      geography = "place",
      variables = my_vars,
      year = .x,
      survey = "acs1",
      geometry = FALSE
    )
  },
  .id = "year" # when combining results, add id var (name of list item)
)
#> Getting data from the 2011 1-year ACS
#> The 1-year ACS provides data for geographies with populations of 65,000 and greater.
#> Getting data from the 2012 1-year ACS
#> The 1-year ACS provides data for geographies with populations of 65,000 and greater.

multi_year %>%
  filter(GEOID == "0644000") %>%
  select(-moe) %>%
  arrange(variable, NAME)
#> # A tibble: 4 × 5
#>   year  GEOID   NAME                         variable    estimate
#>   <chr> <chr>   <chr>                        <chr>          <dbl>
#> 1 2011  0644000 Los Angeles city, California pop_poverty  3748793
#> 2 2012  0644000 Los Angeles city, California pop_poverty  3789330
#> 3 2011  0644000 Los Angeles city, California total_pop    3819708
#> 4 2012  0644000 Los Angeles city, California total_pop    3857786

相关问题