library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(data.table)
##
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
##
## between, first, last
library(ggplot2)
library(tidycensus)
df <- fread("C:/Users/عمر/Desktop/Fall 2024/Rstudio/Week 6/CA_MSA.csv")
# Q1
total_population_for_ma <- df %>%
group_by(NAME) %>%
summarize(total_population = sum(tpop))
#Q2
df$aw <- abs(df$nhasn / df$nhasnc - df$nhwhite / df$nhwhitec)
Result <- df %>%
group_by(NAME) %>%
summarize(aw_diss = 0.5 * sum(aw))
#Q 3
HOLC <- fread("C:/Users/عمر/Desktop/Fall 2024/Rstudio/Week 6/holc_census_tracts.csv")
ave_pct <- HOLC %>%
group_by(st_name) %>%
summarize(avg = mean(holc_area))
#Q4
ggplot(HOLC, aes(x = st_name, y = holc_area)) +
geom_boxplot()
#Q5
TX_HOLC <- HOLC[HOLC$state == "TX" & HOLC$holc_grade == "D", ]
holc_grade_d_Texas_city <- TX_HOLC %>%
group_by(st_name) %>%
summarize(count = n())
#Q6
census_api_key("0d539976d5203a96fa55bbf4421110d4b3db3648", overwrite = TRUE)
## To install your API key for use in future sessions, run this function with `install = TRUE`.
var <- c(poptotal='B03002_001E',
hispanic='B03002_012E',
white='B03002_003E',
black='B03002_004E',
asian='B03002_006E',
poptotal2='B17017_001E',
poverty='B17017_002E')
st <-"TX"
ct <-"Bexar"
ct <- get_acs(geography = "tract", variables = var, count=ct,
state = st,output="wide", year = 2021, geometry = TRUE)
## Getting data from the 2017-2021 5-year ACS
## Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
## | | | 0% | | | 1% | |= | 1% | |= | 2% | |== | 2% | |== | 3% | |== | 4% | |=== | 4% | |=== | 5% | |==== | 5% | |==== | 6% | |===== | 7% | |===== | 8% | |====== | 8% | |====== | 9% | |======= | 9% | |======= | 10% | |======== | 11% | |======== | 12% | |========= | 12% | |========= | 13% | |========== | 14% | |========== | 15% | |=========== | 15% | |=========== | 16% | |============ | 17% | |============ | 18% | |============= | 18% | |============= | 19% | |============== | 19% | |============== | 20% | |=============== | 21% | |=============== | 22% | |================ | 22% | |================ | 23% | |================= | 24% | |================== | 25% | |================== | 26% | |=================== | 27% | |=================== | 28% | |==================== | 28% | |==================== | 29% | |===================== | 29% | |===================== | 30% | |===================== | 31% | |====================== | 31% | |====================== | 32% | |======================= | 33% | |======================== | 34% | |======================== | 35% | |========================= | 35% | |========================= | 36% | |========================== | 37% | |=========================== | 38% | |=========================== | 39% | |============================ | 40% | |============================ | 41% | |============================= | 41% | |============================= | 42% | |=============================== | 45% | |================================ | 45% | |================================== | 48% | |==================================== | 51% | |====================================== | 54% | |====================================== | 55% | |======================================= | 55% | |======================================= | 56% | |======================================== | 57% | |======================================== | 58% | |=================================================== | 73% | |==================================================== | 74% | |==================================================== | 75% | |===================================================== | 75% | |===================================================== | 76% | |====================================================== | 77% | |====================================================== | 78% | |======================================================= | 78% | |======================================================= | 79% | |======================================================== | 80% | |======================================================== | 81% | |========================================================= | 81% | |========================================================= | 82% | |========================================================== | 82% | |========================================================== | 83% | |========================================================== | 84% | |=========================================================== | 84% | |=========================================================== | 85% | |============================================================ | 85% | |============================================================ | 86% | |============================================================= | 86% | |============================================================= | 87% | |=============================================================== | 89% | |=============================================================== | 90% | |================================================================ | 91% | |================================================================ | 92% | |================================================================= | 92% | |================================================================= | 93% | |================================================================== | 94% | |==================================================================== | 97% | |===================================================================== | 98% | |===================================================================== | 99% | |======================================================================| 99% | |======================================================================| 100%
ct$black_pct <- ct$black / ct$poptotal
ct$poverty_pct <- ct$poverty / ct$poptotal2
#Q7
merged_data <- merge(ct, HOLC, by.x = "GEOID", by.y = "geoid")
# merged_data2 <- merge(ct, HOLC) This is the code for all merged data but when i run it my computer doesn't process the data
avg_black_pct_for_HOLC_grades <- merged_data %>%
group_by(holc_grade) %>%
summarize(avg_black_pct = mean(black_pct))
ggplot(avg_black_pct_for_HOLC_grades, aes(x = holc_grade, y = avg_black_pct)) +
geom_bar(stat = "identity")
#Q8
ggplot(merged_data, aes(x = holc_grade, y = holc_area)) +
geom_boxplot() +
theme_minimal()