# Import acs_data.csv
acs_data <- read.csv("acs_data.csv") # (because I continued from a different session)
# Print the first few rows of the acs_data
head(acs_data)
# Add sex column-the data
sex_map <- list(
Male = c("B01001_002", "B01001_003", "B01001_004", "B01001_005",
"B01001_006", "B01001_007", "B01001_008", "B01001_009",
"B01001_010", "B01001_011", "B01001_012", "B01001_013",
"B01001_014", "B01001_015", "B01001_016", "B01001_017",
"B01001_018", "B01001_019", "B01001_020", "B01001_021",
"B01001_022", "B01001_023", "B01001_024"),
Female = c("B01001_026", "B01001_027", "B01001_028", "B01001_029",
"B01001_030", "B01001_031", "B01001_032", "B01001_033",
"B01001_034", "B01001_035", "B01001_036", "B01001_037",
"B01001_038", "B01001_039", "B01001_040", "B01001_041",
"B01001_042", "B01001_043", "B01001_044", "B01001_045",
"B01001_046", "B01001_047", "B01001_048")
)
age_map <- list(
"B01003_001" = "total",
"B01001_003" = "0-4",
"B01001_004" = "5-9",
"B01001_005" = "10-14",
"B01001_006" = "15-17",
"B01001_007" = "18-19",
"B01001_008" = "20",
"B01001_009" = "21",
"B01001_010" = "22-24 years",
"B01001_011" = "25-29 years",
"B01001_012" = "30-34 years",
"B01001_013" = "35-39 years",
"B01001_014" = "40-44 years",
"B01001_015" = "45-49 years",
"B01001_016" = "50-54 years",
"B01001_017" = "55-59 years",
"B01001_018" = "60-61 years",
"B01001_019" = "62-64 years",
"B01001_020" = "65-66 years",
"B01001_021" = "67-69 years",
"B01001_022" = "70-74 years",
"B01001_023" = "75-79 years",
"B01001_024" = "80-84 years",
"B01001_025" = "85+",
"B01001_027" = "0-4",
"B01001_028" = "5-9",
"B01001_029" = "10-14",
"B01001_030" = "15-17",
"B01001_031" = "18-19",
"B01001_032" = "20",
"B01001_033" = "21",
"B01001_034" = "22-24",
"B01001_035" = "25-29",
"B01001_036" = "30-34",
"B01001_037" = "35-39",
"B01001_038" = "40-44",
"B01001_039" = "45-49",
"B01001_040" = "50-54",
"B01001_041" = "55-59",
"B01001_042" = "60-61",
"B01001_043" = "62-64",
"B01001_044" = "65-66",
"B01001_045" = "67-69",
"B01001_046" = "70-74",
"B01001_047" = "75-79",
"B01001_048" = "80-84",
"B01001_049" = "85+"
)
# Add the 'sex' column based on the variable values
acs_data <- acs_data %>%
mutate(sex = case_when(
variable %in% sex_map$Male ~ "M",
variable %in% sex_map$Female ~ "F",
TRUE ~ NA_character_
))
# Add the age column-acs_data based on the age_map
acs_data$age <- sapply(acs_data$variable, function(v) age_map[[v]])
# Rename GEOID column to st_fips
acs_data <- acs_data %>% rename(st_fips = GEOID)
# Rename NAME column to state
acs_data <- acs_data %>% rename(state = NAME)
# Drop variable and x columns
acs_data <- acs_data %>%
select(-variable, -X)
# View the updated acs_data
head(acs_data)