#Tahra Cartwright MidTerm Submission 

#Task1 response: For line 4-9, We are asking R Studio to install tidyverse (library that will help visualize data), followed by assing variables with unique names
# INC2022 = district’s estimated median household income in 2022.
# INC2017 = district’s estimated median household income in 2017.
if(!require(tidyverse))install.packages("tidyverse")
## Loading required package: tidyverse
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidyverse)

Inc2022 <- read.csv("https://drkblake.com/wp-content/uploads/2024/02/Inc2022.csv")

Inc2017 <- read.csv("https://drkblake.com/wp-content/uploads/2024/02/Inc2017.csv")

  #Task2 response: Line 13-19, Our data frame will be from Inc2017 to Inc2022. 
  # We are then merging HHInc2017 and HHInc2022 to for our key variable of IncomeINC
  
  IncomeINC <- left_join(Inc2017,
                         Inc2022,
                         by = join_by(GEOID == GEOID))
head(IncomeINC, 10)
##         GEOID HHInc2017   District          County HHInc2022   Significance
## 1  4702190022     44091 District 1 Cheatham County     59741    Significant
## 2  4702190212     55247 District 2 Cheatham County     73634    Significant
## 3  4702190402     73802 District 3 Cheatham County     89861 Nonsignificant
## 4  4702190592     49731 District 4 Cheatham County     73293    Significant
## 5  4702190782     60793 District 5 Cheatham County     78380    Significant
## 6  4702190972     66750 District 6 Cheatham County     92305    Significant
## 7  4703790038     56258 District 1 Davidson County     75038    Significant
## 8  4703790228     32487 District 2 Davidson County     54346    Significant
## 9  4703790418     49402 District 3 Davidson County     67953    Significant
## 10 4703790608     79381 District 4 Davidson County    106260    Significant
  #Task3  Change is our assigned variable ( This is the total from HHINC2022-HHInc2017)
  
  Change <- IncomeINC %>%
  mutate(Change = HHInc2022 - HHInc2017)
head(Change, 10)
##         GEOID HHInc2017   District          County HHInc2022   Significance
## 1  4702190022     44091 District 1 Cheatham County     59741    Significant
## 2  4702190212     55247 District 2 Cheatham County     73634    Significant
## 3  4702190402     73802 District 3 Cheatham County     89861 Nonsignificant
## 4  4702190592     49731 District 4 Cheatham County     73293    Significant
## 5  4702190782     60793 District 5 Cheatham County     78380    Significant
## 6  4702190972     66750 District 6 Cheatham County     92305    Significant
## 7  4703790038     56258 District 1 Davidson County     75038    Significant
## 8  4703790228     32487 District 2 Davidson County     54346    Significant
## 9  4703790418     49402 District 3 Davidson County     67953    Significant
## 10 4703790608     79381 District 4 Davidson County    106260    Significant
##    Change
## 1   15650
## 2   18387
## 3   16059
## 4   23562
## 5   17587
## 6   25555
## 7   18780
## 8   21859
## 9   18551
## 10  26879
  #Task4 Level is our assigned variable (Change is assigned 3 vaules equaling  assigned lables)
  Level <- Change %>%
  mutate(Level = case_when(Change < 100000 ~ ">$100k",
                           Change == 100000 ~ "$100k",
                           Change > 100000 ~ "$100k+",
                           .default = "Error"))
head(Level, 10)
##         GEOID HHInc2017   District          County HHInc2022   Significance
## 1  4702190022     44091 District 1 Cheatham County     59741    Significant
## 2  4702190212     55247 District 2 Cheatham County     73634    Significant
## 3  4702190402     73802 District 3 Cheatham County     89861 Nonsignificant
## 4  4702190592     49731 District 4 Cheatham County     73293    Significant
## 5  4702190782     60793 District 5 Cheatham County     78380    Significant
## 6  4702190972     66750 District 6 Cheatham County     92305    Significant
## 7  4703790038     56258 District 1 Davidson County     75038    Significant
## 8  4703790228     32487 District 2 Davidson County     54346    Significant
## 9  4703790418     49402 District 3 Davidson County     67953    Significant
## 10 4703790608     79381 District 4 Davidson County    106260    Significant
##    Change  Level
## 1   15650 >$100k
## 2   18387 >$100k
## 3   16059 >$100k
## 4   23562 >$100k
## 5   17587 >$100k
## 6   25555 >$100k
## 7   18780 >$100k
## 8   21859 >$100k
## 9   18551 >$100k
## 10  26879 >$100k
  #Task 5  We are aggrigating data. We are aggregating Level to come out with County and level being compaired
  
  Level <- group_by(Level, County, Level)
head(Level, 20)
## # A tibble: 20 × 8
## # Groups:   County, Level [2]
##         GEOID HHInc2017 District    County   HHInc2022 Significance Change Level
##         <dbl>     <int> <chr>       <chr>        <int> <chr>         <int> <chr>
##  1 4702190022     44091 District 1  Cheatha…     59741 Significant   15650 >$10…
##  2 4702190212     55247 District 2  Cheatha…     73634 Significant   18387 >$10…
##  3 4702190402     73802 District 3  Cheatha…     89861 Nonsignific…  16059 >$10…
##  4 4702190592     49731 District 4  Cheatha…     73293 Significant   23562 >$10…
##  5 4702190782     60793 District 5  Cheatha…     78380 Significant   17587 >$10…
##  6 4702190972     66750 District 6  Cheatha…     92305 Significant   25555 >$10…
##  7 4703790038     56258 District 1  Davidso…     75038 Significant   18780 >$10…
##  8 4703790228     32487 District 2  Davidso…     54346 Significant   21859 >$10…
##  9 4703790418     49402 District 3  Davidso…     67953 Significant   18551 >$10…
## 10 4703790608     79381 District 4  Davidso…    106260 Significant   26879 >$10…
## 11 4703790798     40220 District 5  Davidso…     71189 Significant   30969 >$10…
## 12 4703790988     50871 District 6  Davidso…     82996 Significant   32125 >$10…
## 13 4703791178     46594 District 7  Davidso…     72500 Significant   25906 >$10…
## 14 4703791368     39885 District 8  Davidso…     62232 Significant   22347 >$10…
## 15 4703791558     39769 District 9  Davidso…     54322 Significant   14553 >$10…
## 16 4703791748     47270 District 10 Davidso…     58338 Significant   11068 >$10…
## 17 4703791938     57086 District 11 Davidso…     73158 Significant   16072 >$10…
## 18 4703792128     61720 District 12 Davidso…     85559 Significant   23839 >$10…
## 19 4703792318     46435 District 13 Davidso…     63110 Significant   16675 >$10…
## 20 4703792508     53068 District 14 Davidso…     64380 Significant   11312 >$10…
Level <- summarize(Level,Count = n ())
## `summarise()` has grouped output by 'County'. You can override using the
## `.groups` argument.
head(Level, 10)
## # A tibble: 7 × 3
## # Groups:   County [7]
##   County            Level  Count
##   <chr>             <chr>  <int>
## 1 Cheatham County   >$100k     6
## 2 Davidson County   >$100k    35
## 3 Robertson County  >$100k    12
## 4 Rutherford County >$100k    21
## 5 Sumner County     >$100k    12
## 6 Williamson County >$100k    12
## 7 Wilson County     >$100k    25
  #Task 6  aggregating data.RichDistricts = top earning districts
  
  RichDistricts <- group_by(Level, Count = 100000)
head(Level, 10)
## # A tibble: 7 × 3
## # Groups:   County [7]
##   County            Level  Count
##   <chr>             <chr>  <int>
## 1 Cheatham County   >$100k     6
## 2 Davidson County   >$100k    35
## 3 Robertson County  >$100k    12
## 4 Rutherford County >$100k    21
## 5 Sumner County     >$100k    12
## 6 Williamson County >$100k    12
## 7 Wilson County     >$100k    25
  #Task7  I used setDT which sets values into decending order
  library(data.table)
## 
## Attaching package: 'data.table'
## 
## The following objects are masked from 'package:lubridate':
## 
##     hour, isoweek, mday, minute, month, quarter, second, wday, week,
##     yday, year
## 
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
## 
## The following object is masked from 'package:purrr':
## 
##     transpose
setDT(Level)[order(-Level)]
##               County  Level Count
## 1:   Cheatham County >$100k     6
## 2:   Davidson County >$100k    35
## 3:  Robertson County >$100k    12
## 4: Rutherford County >$100k    21
## 5:     Sumner County >$100k    12
## 6: Williamson County >$100k    12
## 7:     Wilson County >$100k    25
#Task 8 A interesting pattern I noticed with this visulized data, Cheatham county is one of the overall riches county with 6 out of 7 entry being listed in our RichDistricts. I would like to also note that there is a income difference amongst davidsons districts.

```