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
df<- fread("D:/Documents/RFiles/Datasets/CA_MSA.csv")

df$wa<- abs(df$nhasn/df$nhasnc-df$nhwhite/df$nhwhitec)


#1
tpop_all <- df %>% group_by(NAME) %>% summarize("Total Population"=sum(tpop)) #tpop_all = total population of all


#2
wad<-df %>% 
  group_by(NAME) %>%
  summarize("White-Asian Dissimilarity" = 0.5*sum(wa)) #wad = white-asian dissimilarity index


#3
HOLC<- fread("D:/Documents/RFiles/Datasets/holc_census_tracts.csv")

HOLC_aarea <- HOLC %>% group_by(state) %>% summarize("HOLC Average Area"=mean(holc_area)) #HOLC_aarea = HOLC Average Area


#4 
library(ggplot2)

ggplot(HOLC, aes(x = state,y = holc_area, fill = state)) + 
  geom_boxplot() +
  labs(x = "State", y = "HOLC Area", fill = "State")

#5
TX_HOLC_D <- HOLC %>% 
  group_by(st_name) %>% 
  filter(state == "TX", holc_grade == "D") %>%
  summarize(count=n())


#6
library(tidycensus)
census_api_key("6f85b9f82dda6507d580cd15d7eb8b399f4db621",overwrite = "TRUE")
## To install your API key for use in future sessions, run this function with `install = TRUE`.
census_var <- load_variables(2021, 'acs5', cache = TRUE)

var <- c(poptotal='B03002_001E',
         black='B03002_004E',
         poptotal2='B17017_001E',
         poverty='B17017_002E') 
st <-"TX"
ct <-"Bexar"

TX_poverty_b <- get_acs(geography = "tract", variables = var, count=ct,
              state = st, output="wide", geometry = TRUE)
## Getting data from the 2018-2022 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%  |                                                                              |=                                                                     |   2%  |                                                                              |==                                                                    |   2%  |                                                                              |==                                                                    |   3%  |                                                                              |===                                                                   |   4%  |                                                                              |===                                                                   |   5%  |                                                                              |====                                                                  |   5%  |                                                                              |====                                                                  |   6%  |                                                                              |=====                                                                 |   7%  |                                                                              |======                                                                |   9%  |                                                                              |=======                                                               |  10%  |                                                                              |========                                                              |  11%  |                                                                              |=========                                                             |  13%  |                                                                              |==========                                                            |  15%  |                                                                              |============                                                          |  17%  |                                                                              |=============                                                         |  19%  |                                                                              |===============                                                       |  22%  |                                                                              |==================                                                    |  26%  |                                                                              |====================                                                  |  29%  |                                                                              |=======================                                               |  33%  |                                                                              |=========================                                             |  36%  |                                                                              |===========================                                           |  38%  |                                                                              |==============================                                        |  43%  |                                                                              |=====================================                                 |  53%  |                                                                              |=========================================                             |  58%  |                                                                              |============================================                          |  63%  |                                                                              |================================================                      |  68%  |                                                                              |===================================================                   |  73%  |                                                                              |=======================================================               |  78%  |                                                                              |=======================================================               |  79%  |                                                                              |========================================================              |  80%  |                                                                              |=========================================================             |  81%  |                                                                              |==============================================================        |  88%  |                                                                              |===============================================================       |  91%  |                                                                              |===================================================================   |  96%  |                                                                              |===================================================================== |  98%  |                                                                              |======================================================================| 100%
TX_poverty_b$black_pct <- TX_poverty_b$black/TX_poverty_b$poptotal
TX_poverty_b$poverty_pct <- TX_poverty_b$poverty /TX_poverty_b$poptotal2

TX_poverty_b<-TX_poverty_b[,c(1,12,13)]

#7
HOLC_SA <- HOLC[HOLC$st_name == "San Antonio"]
HOLC_SA <- HOLC_SA[,c(2,8,14,18)]


HOLC_SA$geoid <- as.character(HOLC_SA$geoid)

names(HOLC_SA)[3] <-"GEOID"

SA_HOLC_b <-merge(HOLC_SA,TX_poverty_b, by="GEOID")

ggplot(SA_HOLC_b, aes(x = holc_grade)) +
  geom_bar() +
  labs(x = "HOLC Grade", y = "Black Percentage",)

#8
ggplot(HOLC_SA, aes(x = holc_grade, y = holc_area)) + 
  geom_boxplot() +
  labs(x = "HOLC Grade", y = "HOLC Area")