-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpl94171_places_called_eden.R
162 lines (128 loc) · 5.72 KB
/
pl94171_places_called_eden.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
################################################
# PL94171_places_called_eden.r
# Pull places, counties with "Eden" in their name
# Detailed Township Analysis by CCDs.
# - April 3, 2023 --
# - August 25, 2023 --
##################################################
library(tidyverse)
library(tidycensus)
# load the variable list for 2020 into a dataframe
varlist20 <- load_variables(2020,"pl",cache=FALSE)
selvars20 <- c(TotalPop20 = "P2_001N", # Total Population
Hispanic20 = "P2_002N", # Hispanic or Latino
NH_White20 = "P2_005N", # Non-Hispanic, White alone
NH_Black20 = "P2_006N", # Non-Hispanic, Black or African American alone
NH_AIAN20 = "P2_007N", # Non-Hispanic, American Indian, Alaskan Native alone
NH_Asian20 = "P2_008N", # Non-Hispanic, Asian alone
NH_NHOPI20 = "P2_009N", # Non-Hispanic, Native Hawaiian, Other Pac Islander alone
NH_Other20 = "P2_010N", # Non-Hispanic, Other race alone
NH_Multi20 = "P2_011N", # Non-Hispanic, Two-or-More Races
HousingUnits20 = "H1_001N", # Total Housing Units
Occ_DU20 = "H1_002N", # Occupied Housing Units
Vacant_DU20 = "H1_003N") # Vacant Housing Units
#####################################################################
usa_place <- get_decennial(year=2020, sumfile="pl",
geography = "place",
geometry=TRUE, keep_geo_vars=TRUE,
show_call = TRUE,output="wide",
variables = selvars20) %>%
replace(is.na(.),0)
usa_county <- get_decennial(year=2020, sumfile="pl",
geography = "county",
geometry=TRUE, keep_geo_vars=TRUE,
show_call = TRUE,output="wide",
variables = selvars20) %>%
replace(is.na(.),0)
allstates <- c("AK","AL","AR","AZ","CA","CO","CT","DE","FL","GA",
"HI","IA","ID","IL","IN","KS","KY","LA","MA","MD",
"ME","MI","MN","MO","MS","MT","NC","ND","NE","NH",
"NJ","NM","NV","NY","OH","OK","OR","PA","RI","SC",
"SD","TN","TX","UT","VA","VT","WA","WI","WV","WY",
"DC","PR")
usa_cousub <- get_decennial(year=2020, sumfile="pl",
state= allstates,
geography = "county subdivision",
# geometry=TRUE, keep_geo_vars=TRUE,
show_call = TRUE,output="wide",
variables = selvars20) %>%
replace(is.na(.),0)
usa_cousub <- usa_cousub %>%
separate_wider_position(GEOID,c(state=2,county=3,cousub=5))
stusps_code <- usa_county %>%
sf::st_drop_geometry() %>% # drop the geometry
arrange(STUSPS) %>%
group_by(STUSPS) %>%
count(STUSPS) # tally of counties by state!
######################################################################
# Filter places, counties by particular NAMEs of places, counties.
eden1 <- usa_place %>%
filter(NAME.x %in% c("Eden"," Eden","Eden-"))
eden2 <- usa_place %>%
filter(str_detect(NAME.x,"Eden"))
eden3 <- usa_place %>%
filter(grepl("Eden", NAME.x))
olive1 <- usa_place %>%
filter(grepl("Olive", NAME.x))
paradise <- usa_place %>%
filter(grepl("Paradise", NAME.x))
township <- usa_place %>%
filter(grepl("Town", NAME.x))
township2 <- usa_place %>%
filter(grepl("township", NAME.x))
# Analyze the CCD file for Townships.....
township_ccd <- usa_cousub %>%
filter(grepl("township", NAME))
cousub_tally <- usa_cousub %>%
sf::st_drop_geometry() %>% # drop the geometry
group_by(state) %>%
count() %>%
rename(STATEFP=state,n_cousub=n) # tally of cousubs by state!
place_tally <- usa_place %>%
sf::st_drop_geometry() %>% # drop the geometry
group_by(STATEFP) %>%
count() %>%
rename(n_places=n) # tally of PLACES by state!
township_tally <- township_ccd %>%
mutate(STATEFP=state) %>%
group_by(STATEFP) %>%
count() %>%
rename(n_townships=n) # Tally of CCDs with "township" in their names, by state!
states <- usa_county %>%
sf::st_drop_geometry() %>% # drop the geometry
group_by(STATEFP,STATE_NAME) %>%
count() %>%
rename(n_counties=n) # Tally of counties by state
# Join together the state-level tally of counts of county subdivisions,
# townships and places......export to CSV file!!
states2 <- full_join(states,cousub_tally,by="STATEFP") %>%
full_join(.,township_tally,by="STATEFP") %>%
full_join(.,place_tally,by="STATEFP") %>%
replace(is.na(.),0)
setwd("~/Desktop/tidycensus_work/output")
write.csv(states2, "census2020_county_township_cousub_place_tally_by_state.csv")
####################################################################
prairie <- usa_place %>%
filter(grepl("Prairie", NAME.x))
cupcake <- usa_place %>%
filter(grepl("Cupcake", NAME.x))
#
eden4 <- usa_county %>%
filter(grepl("Eden", NAME.x))
###
# Most common county names!
county1 <- usa_county %>%
group_by(NAME.x) %>%
count() %>%
arrange(desc(n)) %>%
sf::st_drop_geometry() # drop the geometry so we can write a nice csv file!
county2 <- left_join(usa_county,county1,by="NAME.x") %>%
arrange(desc(n),NAME.x,GEOID)
# Most common place names!
places1 <- usa_place %>%
group_by(NAME.x) %>%
count() %>%
arrange(desc(n)) %>%
sf::st_drop_geometry() # drop the geometry so we can write a nice csv file!
places2 <- left_join(usa_place,places1,by="NAME.x") %>%
arrange(desc(n),NAME.x,GEOID)