Fox in Summer
Fox in Summer

Reputation: 21

Fail to get the exact female percentage from tidycensus

I use tidycensus to collect demographic data but it seems something wrong since I got 0 female percentage for the zipcode 39309, 87040 and 78712. I also have the same problem, getting some zero values for bachelor's degree percentage, age percentage and population density. The free census api can be obtained here: https://www.census.gov/data/developers.html

Here is my code for female percentage.

library(tidycensus)
library(tidyverse)

# Create the data frame
zipcode_df <- data.frame(as.character(c(39309, 87040, 78712)))

# Rename the column to "zipcode"
colnames(zipcode_df)[1] <- "zipcode"

# Display the result
print(zipcode_df)

# Set up Census  key
census_api_key(mycensuskey)

# Get total population and female population for all zip code tabulation areas (ZCTAs)
population_gender <- get_acs(
  geography = "zcta",
  variables = c("total_population" = "B01003_001E",
                "female_population" = "B01001A_017"),
  year = 2021,
  survey = "acs5",
  output = "wide"
) |>
  mutate(zipcode = as.character(GEOID)) |>
  select(zipcode, total_population, female_populationE) |>
  rename(female_population = female_populationE)

# Calculate the percentage of male population
percentage_female_population <- population_gender %>%
  mutate(percentage_female = (female_population / total_population) * 100) %>%
  select(zipcode, percentage_female)

outcome <- zipcode_df |> left_join(percentage_female_population, by = "zipcode")
> outcome
  zipcode percentage_female
1   39309                 0
2   87040                 0
3   78712                 0

Upvotes: 0

Views: 43

Answers (0)

Related Questions