census_api_key(“74c3d1fe0a1e580a10119351a7382c1f9ceb8c34”)
mydata <- get_acs( geography = “county subdivision”, state = “TN”, variables = c(MyVar_ = “DP04_0142P”), year = 2022, survey = “acs5”, output = “wide”)
mydata <- separate_wider_delim(mydata, NAME, delim = “,”, names = c(“District”, “County”, “State”))
mydata <- filter( mydata, County == “Davidson County” | County == “Cheatham County” | County == “Robertson County” | County == “Rutherford County” | County == “Sumner County” | County == “Williamson County” | County == “Wilson County”)
head(mydata, 10)
write.csv(mydata, “mydata.csv”, row.names = FALSE)
mydata_filtered <- filter(mydata, County == “Rutherford County”) head(mydata_filtered,10)
mydata_sorted <- arrange(mydata_filtered, desc(MyVar_E)) head(mydata_sorted,10)
summary (mydata_sorted)
library(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(tidycensus)
mydata <- get_acs(
geography = "county subdivision",
state = "TN",
variables = c(MyVar_ = "DP04_0142P"),
year = 2022,
survey = "acs5",
output = "wide")
## Getting data from the 2018-2022 5-year ACS
## Using the ACS Data Profile
mydata <-
separate_wider_delim(mydata,
NAME,
delim = ", ",
names = c("District", "County", "State"))
mydata <- filter(
mydata,
County == "Davidson County" |
County == "Cheatham County" |
County == "Robertson County" |
County == "Rutherford County" |
County == "Sumner County" |
County == "Williamson County" |
County == "Wilson County")
head(mydata, 10)
## # A tibble: 10 × 6
## GEOID District County State MyVar_E MyVar_M
## <chr> <chr> <chr> <chr> <dbl> <dbl>
## 1 4702190022 District 1 Cheatham County Tennessee 31.4 12.3
## 2 4702190212 District 2 Cheatham County Tennessee 34.7 23
## 3 4702190402 District 3 Cheatham County Tennessee 27.9 18
## 4 4702190592 District 4 Cheatham County Tennessee 22.6 17.5
## 5 4702190782 District 5 Cheatham County Tennessee 36.9 17.9
## 6 4702190972 District 6 Cheatham County Tennessee 8.1 10.4
## 7 4703790038 District 1 Davidson County Tennessee 39.7 16.9
## 8 4703790228 District 2 Davidson County Tennessee 39.3 8.4
## 9 4703790418 District 3 Davidson County Tennessee 52.6 13.3
## 10 4703790608 District 4 Davidson County Tennessee 25.6 7.9
write.csv(mydata, "mydata.csv", row.names = FALSE)
mydata_filtered <- filter(mydata, County == "Rutherford County")
head(mydata_filtered,10)
## # A tibble: 10 × 6
## GEOID District County State MyVar_E MyVar_M
## <chr> <chr> <chr> <chr> <dbl> <dbl>
## 1 4714990150 District 1 Rutherford County Tennessee 35.6 13.6
## 2 4714990340 District 2 Rutherford County Tennessee 28.8 27.9
## 3 4714990530 District 3 Rutherford County Tennessee 54 15.2
## 4 4714990720 District 4 Rutherford County Tennessee 47.3 15.2
## 5 4714990910 District 5 Rutherford County Tennessee 41.5 10.5
## 6 4714991100 District 6 Rutherford County Tennessee 21.8 14.7
## 7 4714991290 District 7 Rutherford County Tennessee 45.9 10.7
## 8 4714991480 District 8 Rutherford County Tennessee 36.6 18.5
## 9 4714991670 District 9 Rutherford County Tennessee 46.9 25
## 10 4714991860 District 10 Rutherford County Tennessee 40.9 16.9
mydata_sorted <- arrange(mydata_filtered, desc(MyVar_E))
head(mydata_sorted,10)
## # A tibble: 10 × 6
## GEOID District County State MyVar_E MyVar_M
## <chr> <chr> <chr> <chr> <dbl> <dbl>
## 1 4714990530 District 3 Rutherford County Tennessee 54 15.2
## 2 4714993190 District 17 Rutherford County Tennessee 49.2 8
## 3 4714993570 District 19 Rutherford County Tennessee 48.3 15.9
## 4 4714993760 District 20 Rutherford County Tennessee 48.3 13.4
## 5 4714990720 District 4 Rutherford County Tennessee 47.3 15.2
## 6 4714991670 District 9 Rutherford County Tennessee 46.9 25
## 7 4714991290 District 7 Rutherford County Tennessee 45.9 10.7
## 8 4714990910 District 5 Rutherford County Tennessee 41.5 10.5
## 9 4714991860 District 10 Rutherford County Tennessee 40.9 16.9
## 10 4714991480 District 8 Rutherford County Tennessee 36.6 18.5