-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr_script
1915 lines (1078 loc) · 104 KB
/
r_script
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Summary report on Census data about veterans"
author: "Jennifer Peebles for The Atlanta Journal-Constitution"
date: "10/9/2021"
output:
html_document: default
pdf_document: default
editor_options:
chunk_output_type: console
---
```{r setup, echo=FALSE, warning=FALSE, message=FALSE, error=FALSE}
# Let's load a ton of libraries. Many of these are probably duplicative and are included in the tidyverse package. My apologies.
library(tidyverse)
library(knitr)
library(stringr)
library(dplyr) # in tidyverse
library(ggplot2)
library(swirl)
library(digest)
library(magrittr)
library(tidycensus) # for getting Census data from the Census API
options(scipen = 999) # this will stop R from putting big numbers into scientific notation.
# Plug in your Census API key here between the quote marks
census_api_key("YOUR_CENSUS_API_KEY_GOES_HERE")
# This knit_hooks item includes the comma as the thousand separator in the inline code snippet numbers.
# More on this at https://stackoverflow.com/questions/18965637/set-global-thousand-separator-on-knitr
knit_hooks$set(inline = function(x) {
prettyNum(x, big.mark=",")
})
knitr::opts_chunk$set(echo = TRUE)
options(digits=1)
```
```{r some_preparation_before_we_go_further, eval=TRUE, echo=FALSE, include=FALSE}
# Get R to tell you what your working directory is.
getwd()
# If you have a hankering to change your working directory, you can un-comment this next line and plop in the file path. Remember to make sure to use forward slashes, or else R will read the slashes as escape characters!
# setwd("my_file_path_here")
# Plug in the year here.
our_year <- 2019
# Plug in the name of your state here -- the full name, no abbreviations, capitalize correctly. If you live in D.C., put "District of Columbia." If you live in Puerto Rico, put "Puerto Rico" as you normally would.
my_state <- "Georgia"
# This next line will take the name of our state and turn it into a string that can be used in a regular expression in certain instances in the code.
my_state_regexp_string <- paste0(my_state, "$", collapse="") %>% print()
```
```{r total_veteran_population_national, eval=TRUE, echo=FALSE, include=FALSE}
# Let's start by pulling down the variables for the 2019 and 2010 ACS.
v19 <- load_variables(2019, "acs5", cache = TRUE)
v10 <- load_variables(2010, "acs5", cache = TRUE)
# Now, let's pull info about the total veteran population for the U.S. We're going to first look at ACS table B21001. This looks at veterans as a share of the civilian population 18 and over.
# The variable for the estimated number of civilians 18 and over is B21001_001. B21001_002 is the estimated number of veterans in that larger group.
# We'll use tidycensus to pull down the numbers we need from the Census API. Let's get the nationwide figures first.
us_total_vets <- get_acs(geography = "US", year = our_year, survey = "acs5", variables = c("B21001_001", "B21001_002"), geometry = FALSE) %>% select(-moe) %>% pivot_wider(names_from=variable, values_from = estimate, names_sep="_") %>% print()
us_total_vets <- us_total_vets %>% rename("total_pop18nover"=B21001_001, "vet_pop"=B21001_002) %>% mutate(pct_vet = ((vet_pop/total_pop18nover)*100)) %>% print()
```
## Overall summary
All data in this summary comes from the `r our_year` 5-year American Community Survey and was pulled down from the Census Bureau's computers via the Census' Application Programming Interface (API) using the [R programming language](https://en.wikipedia.org/wiki/R_(programming_language)) and the R add-on package called [tidycensus](https://walker-data.com/tidycensus/) by Kyle Walker. Among numerous other packages, this script also relies heavily on packages that are part of the [tidyverse](https://www.tidyverse.org/) family created by Hadley Wickham et al. Thank-you to all the folks who authored all that code and keep it maintained!
## Total veteran population
#### In the U.S.
The U.S. has `r us_total_vets$total_pop18nover` people who are 18 and over, according to estimates from the 2019 5-year American Community Survey.
Of that group, `r us_total_vets$vet_pop` are veterans, the ACS says. That's `r us_total_vets$pct_vet`%.
```{r total_vet_population_states, eval=TRUE, echo=FALSE, include=FALSE}
# So, we've done the national-level data. Now, let's get the state-by-state totals on total vets.
all_states_total_vets <- get_acs(geography = "state", year = our_year, survey = "acs5", variables = c("B21001_001", "B21001_002"), geometry = FALSE) %>% select(-moe) %>% pivot_wider(names_from=variable, values_from = estimate, names_sep="_") %>% print()
all_states_total_vets <- all_states_total_vets %>% rename("total_pop18nover"=B21001_001, "vet_pop"=B21001_002) %>% mutate(pct_vet = ((vet_pop/total_pop18nover)*100)) %>% print()
all_states_total_vets$rank <- rank(-all_states_total_vets$pct_vet, na.last=TRUE, ties.method = "min") %>% print()
my_state_total_vets <- all_states_total_vets %>% filter(str_detect(NAME, my_state)) %>% print()
# So, where does your state rank among the 50 states in its percentage of overall veterans? Keep in mind, I think this table includes all 50 states plus the District of Columbia and Puerto Rico, so the last position in the list is actually No. 52.
# In the next section, we'll pull total veteran population by county for our state.
```
#### In `r my_state`
In the 2019 5-year ACS, `r my_state` had a population 18 and over of `r my_state_total_vets$total_pop18nover`. Of that group, `r my_state_total_vets$vet_pop` were veterans. That's about `r my_state_total_vets$pct_vet`%. `r my_state` ranked `r my_state_total_vets$rank`th of the 50 states, DC and Puerto Rico for its percentage of veterans.
```{r total_vet_population_counties, eval=TRUE, echo=FALSE, include=FALSE}
# So, let's look at counties ranked nationally for total vet population.
all_county_total_vets <- get_acs(geography = "county", year = our_year, survey = "acs5", variables = c("B21001_001", "B21001_002"), geometry = FALSE) %>% select(-moe) %>% pivot_wider(names_from=variable, values_from = estimate, names_sep="_") %>% print()
all_county_total_vets <- all_county_total_vets %>% rename("total_pop18nover"=B21001_001, "vet_pop"=B21001_002) %>% mutate(pct_vet = ((vet_pop/total_pop18nover)*100)) %>% print()
all_county_total_vets$rank_in_total_vet_pop <- rank(-all_county_total_vets$pct_vet, na.last = TRUE, ties.method = c("min")) %>% print()
my_state_counties_total_vets <- all_county_total_vets %>% filter(str_detect(NAME, my_state)) %>% print()
# Here's something on my to-do list: I want to show each county not just by rank but by percentile.
# More about dplyr's slice functions at https://dplyr.tidyverse.org/reference/slice.html
top_counties_my_state_vets <- my_state_counties_total_vets %>% rename("County"=NAME, "Total population 18 and over"=total_pop18nover, "Veteran population"=vet_pop, "Percent of population 18 and over who are veterans"=pct_vet, "Rank (nationally) in veteran population"=rank_in_total_vet_pop) %>% select(-GEOID) %>% slice_max(`Percent of population 18 and over who are veterans`, n=5) %>% print()
bottom_counties_my_state_vets <- my_state_counties_total_vets %>% rename("County"=NAME, "Total population 18 and over"=total_pop18nover, "Veteran population"=vet_pop, "Percent of population 18 and over who are veterans"=pct_vet, "Rank (nationally) in veteran population"=rank_in_total_vet_pop) %>% select(-GEOID) %>% slice_min(`Percent of population 18 and over who are veterans`, n=5) %>% arrange(-desc(`Rank (nationally) in veteran population`)) %>% print()
```
## `r my_state` counties by total vets
It's interesting to see which counties have the highest concentrations of veterans. Many of the counties at the top of the list are near military bases.
#### Highest percentages
`r knitr::kable(top_counties_my_state_vets)`
#### Lowest percentages
`r knitr::kable(bottom_counties_my_state_vets)`
```{r total_vet_population_places, eval=TRUE, echo=FALSE, include=FALSE}
# Now, let's look at total veteran population by "place." We'll remove the Census-designated places (CDPs) from the data in hopes we get a set of just cities and towns. We'll also filter down to places with at least 100 residents.
all_place_total_vets <- get_acs(geography = "place", year = our_year, survey = "acs5", variables = c("B21001_001", "B21001_002"), geometry = FALSE) %>% select(-moe) %>% pivot_wider(names_from=variable, values_from = estimate, names_sep="_") %>% filter(!str_detect(NAME, "CDP")) %>% filter(B21001_001>=100) %>% print()
all_place_total_vets <- all_place_total_vets %>% rename("total_pop18nover"=B21001_001, "vet_pop"=B21001_002) %>% mutate(pct_vet = ((vet_pop/total_pop18nover)*100)) %>% print()
all_place_total_vets$rank_in_total_vet_pop <- rank(-all_place_total_vets$pct_vet, na.last = TRUE, ties.method = c("min")) %>% print()
my_state_places_total_vets <- all_place_total_vets %>% filter(str_detect(NAME, my_state_regexp_string)) %>% print()
top_places_my_state_vets <- my_state_places_total_vets %>% rename("Place"=NAME, "Total population 18 and over"=total_pop18nover, "Veteran population"=vet_pop, "Percent of population 18 and over who are veterans"=pct_vet, "Rank (nationally) in veteran population"=rank_in_total_vet_pop) %>% select(-GEOID) %>% slice_max(`Percent of population 18 and over who are veterans`, n=5) %>% print()
```
### `r my_state` places by total vets
This query filtered down to just places in the Census data with 100 or more residents. It also excluded any place in the Census data identified as a "CDP" -- [Census-designated place](https://en.wikipedia.org/wiki/Census-designated_place), which generally means it's not incorporated. Hopefully this has removed most of the "places" in the data that were not incorporated cities or towns, but there are always some exceptions every year, so be aware of that.
`r knitr::kable(top_places_my_state_vets)`
```{r vets_by_war_period_national, eval=TRUE, echo=FALSE, include=FALSE}
# OK, let's move on now from that table. Let's move on to another ACS table that breaks down the veteran population by the war or wartime period in which they served (WWII, Korea, Vietnam, Gulf War, post-9/11, etc). This is going to be table B21002.
# Our first variable in the table will be B21002_001, and the variables will go up sequentially all the way to _016. Yikes.
all_america_vets_by_war <- get_acs(geography = "us", year = our_year, survey = "acs5", variables = c("B21001_001", "B21002_002", "B21002_003", "B21002_004", "B21002_005", "B21002_006", "B21002_007", "B21002_008", "B21002_009", "B21002_010", "B21002_011", "B21002_012", "B21002_013", "B21002_014", "B21002_015", "B21002_016"), geometry = FALSE) %>% select(-moe) %>% pivot_wider(names_from=variable, values_from = estimate, names_sep="_") %>% print()
# The column names here are darn confusing. Let's rename them.
colnames(all_america_vets_by_war) <- c("geoid",
"name",
"total_population_vet_and_nonvet",
"post_911_service_only",
"post911andgulf_noviet",
"post911andgulf_andviet",
"gulfnotvietnam",
"gulf_and_vietnam",
"vietnam_only",
"viet_and_korea",
"vietkoreawwii",
"koreaonly",
"koreawwii_noviet",
"wwiionly",
"betweengulfandviet",
"bw_korea_and_viet",
"bw_wwii_and_korea",
"prewwii_only")
# So, here's a tricky part.
# As a newspaper reporter, I have a strong sense that one question many reporters will say, "America is losing its 'Greatest Generation.' How many World War II-era veterans are left in my community?" The trouble is, the Census Bureau breaks vets up into all the above-mentioned categories, because there are so many combinations.
# To count the total number of WWII-era vets, we have to add up the numbers of people who served only in the WWII era, plus the number who served in both WWII and Korea, and then add everyone who served in WWII and Vietnam. Whew! And then we have to do the same combining for all the various wars -- Korea, Vietnam, 9/11.
# So, time for some addition ...
all_america_vets_by_war2 <- all_america_vets_by_war %>% mutate(wwii_service_any_combo = (wwiionly + koreawwii_noviet + vietkoreawwii), korea_service_any_combo = (koreawwii_noviet + koreaonly + vietkoreawwii + viet_and_korea), vietnam_service_any_combo = (vietkoreawwii + viet_and_korea + vietnam_only + gulf_and_vietnam + post911andgulf_andviet), gulf_service_any_combo = (gulf_and_vietnam + gulfnotvietnam + post911andgulf_andviet + post911andgulf_noviet), post_911_any_combo = (post911andgulf_andviet + post911andgulf_noviet + post_911_service_only)) %>% select(-geoid, -name)
# Time to assign some nice, neat names that will make sense in English.
colnames(all_america_vets_by_war2) <- c("Total population 18 and over, veteran and non-veteran", #1
"Vets with Post-9-11 service only", #2
"Served in Gulf War and post-9-11", #3
"Served in Vietnam, Gulf War and post-9-11", #4
"Gulf War service only", #5
"Vietnam and Gulf War service", #6
"Vietnam War service only", #7
"Korea and Vietnam service", #8
"Served in WWII, Korea and Vietnam",
"Korean War service only",
"Served in World War II and Korean War",
"World War II service only",
"Served between Vietnam and the Gulf War",
"Served between Korea and Vietnam",
"Served between WWII and Korea",
"Pre-WWII service only",
"World War II service, alone or in combination with any other war",
"Korean War service, alone or in combination with any other war",
"Vietnam War service, alone or in combination with any other war",
"Gulf War service, alone or in combination with any other war",
"Post-9-11 service, alone or in combination with any other war")
# The t command transposes the table.
us_all_vets_by_war_kable <- t(all_america_vets_by_war2)
```
## Veterans by war or period of service
### In the U.S.
Note: This only groups the veterans by the period in which they served. For instance, a veteran who is reported in the Census data as having "Vietnam War-era service" only means they served in the military during the time of the Vietnam War; it doesn't mean they actually ever were physically sent to Vietnam.
Veterans in the categories tallied by the Census Bureau aren't duplicated from period to period. But because our initial audience here is one of journalists, we've taken the liberty of creating five new categories that add up the total number of vets who served in the five major wars since WWII: WWII, Korea, Vietnam, the Gulf War, and post-9/11. Those five categories all end with the phrase "alone or in combination with any other war." Be aware that vets can be duplicated between those five categories.
`r knitr::kable(us_all_vets_by_war_kable)`
```{r vets_by_war_period_states, eval=TRUE, echo=FALSE, include=FALSE}
# Now, to repeat the same steps, but this time, at the state level.
all_state_vets_by_war <- get_acs(geography = "state", year = our_year, survey = "acs5", variables = c("B21001_001", "B21002_002", "B21002_003", "B21002_004", "B21002_005", "B21002_006", "B21002_007", "B21002_008", "B21002_009", "B21002_010", "B21002_011", "B21002_012", "B21002_013", "B21002_014", "B21002_015", "B21002_016"), geometry = FALSE) %>% select(-moe) %>% pivot_wider(names_from=variable, values_from = estimate, names_sep="_") %>% print()
colnames(all_state_vets_by_war) <- c("geoid",
"name",
"total_population_vet_and_nonvet",
"post_911_service_only",
"post911andgulf_noviet",
"post911andgulf_andviet",
"gulfnotvietnam",
"gulf_and_vietnam",
"vietnam_only",
"viet_and_korea",
"vietkoreawwii",
"koreaonly",
"koreawwii_noviet",
"wwiionly",
"betweengulfandviet",
"bw_korea_and_viet",
"bw_wwii_and_korea",
"prewwii_only")
# Let's add up the major wars again ...
all_state_vets_by_war2 <- all_state_vets_by_war %>% mutate(wwii_service_any_combo = (wwiionly + koreawwii_noviet + vietkoreawwii), korea_service_any_combo = (koreawwii_noviet + koreaonly + vietkoreawwii + viet_and_korea), vietnam_service_any_combo = (vietkoreawwii + viet_and_korea + vietnam_only + gulf_and_vietnam + post911andgulf_andviet), gulf_service_any_combo = (gulf_and_vietnam + gulfnotvietnam + post911andgulf_andviet + post911andgulf_noviet), post_911_any_combo = (post911andgulf_andviet + post911andgulf_noviet + post_911_service_only))
all_state_vets_by_war_period_totals <- all_state_vets_by_war2
my_state_vets_by_war_period_totals <- all_state_vets_by_war_period_totals %>% filter(str_detect(name, my_state_regexp_string)) %>% select(-geoid, -name) %>% print()
colnames(my_state_vets_by_war_period_totals) <- c("Total population 18 and over, veteran and non-veteran", #1
"Vets with Post-9-11 service only", #2
"Served in Gulf War and post-9-11", #3
"Served in Vietnam, Gulf War and post-9-11", #4
"Gulf War service only", #5
"Vietnam and Gulf War service", #6
"Vietnam War service only", #7
"Korea and Vietnam service", #8
"Served in WWII, Korea and Vietnam",
"Korean War service only",
"Served in World War II and Korean War",
"World War II service only",
"Served between Vietnam and the Gulf War",
"Served between Korea and Vietnam",
"Served between WWII and Korea",
"Pre-WWII service only",
"World War II service, alone or in combination with any other war",
"Korean War service, alone or in combination with any other war",
"Vietnam War service, alone or in combination with any other war",
"Gulf War service, alone or in combination with any other war",
"Post-9-11 service, alone or in combination with any other war")
# We're going to have to do some woogie-ing with the columns in the table to get everything lined up right for presentation. ...
my_state_vets_by_war_period_totals_kable1 <- t(my_state_vets_by_war_period_totals) %>% print()
my_state_vets_by_war_period_totals_kable2 <- data.frame(my_state_vets_by_war_period_totals_kable1)
colnames(my_state_vets_by_war_period_totals_kable2) <- c("Value")
my_state_vets_by_war_period_totals_kable2$Period <- rownames(my_state_vets_by_war_period_totals_kable2)
my_state_vets_by_war_period_totals_kable3 <-my_state_vets_by_war_period_totals_kable2[,c(2,1)]
rownames(my_state_vets_by_war_period_totals_kable3) <- NULL
my_state_vets_by_war_period_totals_kable <- my_state_vets_by_war_period_totals_kable3
all_state_vets_by_war_pcts <- all_state_vets_by_war2 %>% mutate(across(4:23, ~ (.x/all_state_vets_by_war2$total_population_vet_and_nonvet))*100)
my_state_vets_by_war_period_pcts <- all_state_vets_by_war_pcts %>% filter(str_detect(name, my_state_regexp_string)) %>% select(-geoid, -name) %>% print()
colnames(my_state_vets_by_war_period_pcts) <- colnames(my_state_vets_by_war_period_totals) <- c("Total population 18 and over, veteran and non-veteran", #1
"Vets with Post-9-11 service only", #2
"Served in Gulf War and post-9-11", #3
"Served in Vietnam, Gulf War and post-9-11", #4
"Gulf War service only", #5
"Vietnam and Gulf War service", #6
"Vietnam War service only", #7
"Korea and Vietnam service", #8
"Served in WWII, Korea and Vietnam",
"Korean War service only",
"Served in World War II and Korean War",
"World War II service only",
"Served between Vietnam and the Gulf War",
"Served between Korea and Vietnam",
"Served between WWII and Korea",
"Pre-WWII service only",
"World War II service, alone or in combination with any other war",
"Korean War service, alone or in combination with any other war",
"Vietnam War service, alone or in combination with any other war",
"Gulf War service, alone or in combination with any other war",
"Post-9-11 service, alone or in combination with any other war")
my_state_vets_by_war_period_pcts_kable1 <- t(my_state_vets_by_war_period_pcts) %>% print()
my_state_vets_by_war_period_pcts_kable2 <- data.frame(my_state_vets_by_war_period_pcts_kable1)
my_state_vets_by_war_period_pcts_kable2$Item <- rownames(my_state_vets_by_war_period_pcts_kable2) %>% print()
colnames(my_state_vets_by_war_period_pcts_kable2) <- c("Value", "Item")
my_state_vets_by_war_period_pcts_kable2 <- my_state_vets_by_war_period_pcts_kable2[,c(2,1)]
rownames(my_state_vets_by_war_period_pcts_kable2) <- NULL
my_state_vets_by_war_period_pcts_kable <- my_state_vets_by_war_period_pcts_kable2 %>% filter(!str_detect(Item, "Total population 18")) %>% print()
all_state_vets_by_war_rank <- all_state_vets_by_war_pcts %>% mutate(across(4:23, ~ rank(-.x, na.last = TRUE, ties.method = c("min")))) %>% print()
my_state_vets_by_war_period_rank <- all_state_vets_by_war_rank %>% filter(str_detect(name, my_state_regexp_string)) %>% select(-geoid,-name, -total_population_vet_and_nonvet) %>% print()
colnames(my_state_vets_by_war_period_rank)<- c(
"Vets with Post-9-11 service only", #2
"Served in Gulf War and post-9-11", #3
"Served in Vietnam, Gulf War and post-9-11", #4
"Gulf War service only", #5
"Vietnam and Gulf War service", #6
"Vietnam War service only", #7
"Korea and Vietnam service", #8
"Served in WWII, Korea and Vietnam",
"Korean War service only",
"Served in World War II and Korean War",
"World War II service only",
"Served between Vietnam and the Gulf War",
"Served between Korea and Vietnam",
"Served between WWII and Korea",
"Pre-WWII service only",
"World War II service, alone or in combination with any other war",
"Korean War service, alone or in combination with any other war",
"Vietnam War service, alone or in combination with any other war",
"Gulf War service, alone or in combination with any other war",
"Post-9-11 service, alone or in combination with any other war")
my_state_vets_by_war_period_rank_kable1 <- t(my_state_vets_by_war_period_rank) %>% print()
my_state_vets_by_war_period_rank_kable2 <- data.frame(my_state_vets_by_war_period_rank_kable1)
my_state_vets_by_war_period_rank_kable2$Period <- rownames(my_state_vets_by_war_period_rank_kable2)
my_state_vets_by_war_period_rank_kable3 <- my_state_vets_by_war_period_rank_kable2[,c(2,1)]
colnames(my_state_vets_by_war_period_rank_kable3) <- c("Period", "Rank nationally")
rownames(my_state_vets_by_war_period_rank_kable3) <- NULL
my_state_vets_by_war_period_rank_kable <- my_state_vets_by_war_period_rank_kable3
```
### In `r my_state`
#### Raw totals
Note: This only groups the veterans by the period in which they served. A veteran who is reported in the Census data as having "Vietnam War-era service" only means they served in the military during the time of the Vietnam War; it doesn't mean they actually ever were physically sent to Vietnam.
All data here from the 2019 5-year ACS.
`r knitr::kable(my_state_vets_by_war_period_totals_kable, digits=1, col.names=c("Item","Value"), format.args = list(big.mark = ",", scientific = FALSE))`
#### By percentage of the state's total population 18 and over
`r knitr::kable(my_state_vets_by_war_period_pcts_kable, digits=1, col.names=c("Item","Value"), format.args = list(big.mark = ",", scientific = FALSE))`
#### `r my_state`'s rank among the 50 states
`r knitr::kable(my_state_vets_by_war_period_rank_kable, digits=1, col.names=c("Period","Rank nationally"), format.args = list(big.mark = ",", scientific = FALSE))`
```{r vets_by_war_period_county, eval=TRUE, echo=FALSE, include=FALSE}
# Now, we breakdown the veterans by wartime period of service by county.
all_county_vets_by_war <- get_acs(geography = "county", year = our_year, survey = "acs5", variables = c("B21001_001", "B21002_002", "B21002_003", "B21002_004", "B21002_005", "B21002_006", "B21002_007", "B21002_008", "B21002_009", "B21002_010", "B21002_011", "B21002_012", "B21002_013", "B21002_014", "B21002_015", "B21002_016"), geometry = FALSE) %>% select(-moe) %>% pivot_wider(names_from=variable, values_from = estimate, names_sep="_") %>% print()
colnames(all_county_vets_by_war) <- c("geoid",
"name",
"total_population_vet_and_nonvet",
"post_911_service_only",
"post911andgulf_noviet",
"post911andgulf_andviet",
"gulfnotvietnam",
"gulf_and_vietnam",
"vietnam_only",
"viet_and_korea",
"vietkoreawwii",
"koreaonly",
"koreawwii_noviet",
"wwiionly",
"betweengulfandviet",
"bw_korea_and_viet",
"bw_wwii_and_korea",
"prewwii_only")
all_county_vets_by_war2 <- all_county_vets_by_war %>% mutate(wwii_service_any_combo = (wwiionly + koreawwii_noviet + vietkoreawwii), korea_service_any_combo = (koreawwii_noviet + koreaonly + vietkoreawwii + viet_and_korea), vietnam_service_any_combo = (vietkoreawwii + viet_and_korea + vietnam_only + gulf_and_vietnam + post911andgulf_andviet), gulf_service_any_combo = (gulf_and_vietnam + gulfnotvietnam + post911andgulf_andviet + post911andgulf_noviet), post_911_any_combo = (post911andgulf_andviet + post911andgulf_noviet + post_911_service_only))
all_county_vets_by_war_totals <- all_county_vets_by_war2
my_state_county_vets_by_war_totals <- all_county_vets_by_war_totals %>% filter(str_detect(name, my_state_regexp_string)) %>% print()
all_county_vets_by_war3 <- all_county_vets_by_war2 %>% mutate(across(4:23, ~ (.x/all_county_vets_by_war$total_population_vet_and_nonvet))*100)
all_county_vets_by_war_pcts <- all_county_vets_by_war3
my_state_county_vets_by_war_pcts <- all_county_vets_by_war_pcts %>% filter(str_detect(name, my_state_regexp_string)) %>% print()
all_county_vets_by_war4 <- all_county_vets_by_war3 %>% mutate(across(4:23, ~ rank(-.x, na.last = TRUE, ties.method = c("min")))) %>% print()
colnames(all_county_vets_by_war4) <- c("geoid",
"name",
"total_population_vet_and_nonvet",
"post_911_service_only",
"post911andgulf_noviet",
"post911andgulf_andviet",
"gulfnotvietnam",
"gulf_and_vietnam",
"vietnam_only",
"viet_and_korea",
"vietkoreawwii",
"koreaonly",
"koreawwii_noviet",
"wwiionly",
"betweengulfandviet",
"bw_korea_and_viet",
"bw_wwii_and_korea",
"prewwii_only",
"wwii_service_any_combo",
"korea_service_any_combo",
"vietnam_service_any_combo",
"gulf_service_any_combo",
"post_911_any_combo"
)
all_county_vets_by_war_rank <- all_county_vets_by_war4
my_state_county_vets_by_war_rank <- all_county_vets_by_war_rank %>% filter(str_detect(name, my_state_regexp_string)) %>% print()
which((my_state_county_vets_by_war_rank[2:23] <= 5), arr.ind=TRUE)
my_state_county_vets_by_war_rank[,c(2,4)] %>% slice_min(post_911_service_only, n =5)
my_state_county_vets_by_war_rank[,c(2,5)] %>% slice_min(post911andgulf_noviet, n =5)
my_state_county_vets_by_war_rank[,c(2,6)] %>% slice_min(post911andgulf_andviet, n =5)
my_state_county_vets_by_war_rank[,c(2,23)] %>% slice_min(post_911_any_combo, n =5)
```
### Veterans in `r my_state` counties by war or period of service
Ordered by their national rank
#### By veterans with post-9/11 service only
`r kable(my_state_county_vets_by_war_rank %>% slice_min(post_911_service_only, n =5) %>% select(name, post_911_service_only), digits=1, col.names=c("County","Rank nationally"), format.args = list(big.mark = ",", scientific = FALSE))`
#### By vets with both Gulf War and post-9/11 service
`r kable(my_state_county_vets_by_war_rank %>% slice_min(post911andgulf_noviet, n =5) %>% select(name, post911andgulf_noviet), digits=1, col.names=c("County","Rank nationally"), format.args = list(big.mark = ",", scientific = FALSE))`
#### By vets who served in the Vietnam era, the Gulf War and post-9/11
Note: This is not a huge group of people nationally to start with. The American involvement in Vietnam ended in about 1973, and the Sept. 11 terrorist attacks were 28 years later.
`r kable(my_state_county_vets_by_war_rank %>% slice_min(post911andgulf_andviet, n =5) %>% select(name, post911andgulf_andviet), digits=1, col.names=c("County","Rank nationally"), format.args = list(big.mark = ",", scientific = FALSE))`
#### By vets who served since 9/11, alone or in combination with any other wartime period
`r kable(my_state_county_vets_by_war_rank %>% slice_min(post_911_any_combo, n =5) %>% select(name, post_911_any_combo), digits=1, col.names=c("County","Rank nationally"), format.args = list(big.mark = ",", scientific = FALSE))`
#### By vets who served in the Gulf War period, alone or in combination with any other wartime period
`r kable(my_state_county_vets_by_war_rank %>% slice_min(gulf_service_any_combo, n =5) %>% select(name, gulf_service_any_combo), digits=1, col.names=c("County","Rank nationally"), format.args = list(big.mark = ",", scientific = FALSE))`
#### By vets who served in the Vietnam War period, alone or in combination with any other wartime period
`r kable(my_state_county_vets_by_war_rank %>% slice_min(vietnam_service_any_combo, n =5) %>% select(name, vietnam_service_any_combo), digits=1, col.names=c("County","Rank nationally"), format.args = list(big.mark = ",", scientific = FALSE))`
#### By vets who served in the Korean War period, alone or in combination with any other wartime period
`r kable(my_state_county_vets_by_war_rank %>% slice_min(korea_service_any_combo, n =5) %>% select(name, korea_service_any_combo), digits=1, col.names=c("County","Rank nationally"), format.args = list(big.mark = ",", scientific = FALSE))`
#### By vets who served in the World War II era, alone or in combination with any other wartime period
`r kable(my_state_county_vets_by_war_rank %>% slice_min(wwii_service_any_combo, n =5) %>% select(name, wwii_service_any_combo), digits=1, col.names=c("County","Rank nationally"), format.args = list(big.mark = ",", scientific = FALSE))`
```{r vets_by_war_period_place, eval=TRUE, echo=FALSE, include=FALSE}
all_places_vets_by_war <- get_acs(geography = "place", year = our_year, survey = "acs5", variables = c("B21001_001", "B21002_002", "B21002_003", "B21002_004", "B21002_005", "B21002_006", "B21002_007", "B21002_008", "B21002_009", "B21002_010", "B21002_011", "B21002_012", "B21002_013", "B21002_014", "B21002_015", "B21002_016"), geometry = FALSE) %>% select(-moe) %>% pivot_wider(names_from=variable, values_from = estimate, names_sep="_") %>% print()
colnames(all_places_vets_by_war) <- c("geoid",
"name",
"total_population_vet_and_nonvet",
"post_911_service_only",
"post911andgulf_noviet",
"post911andgulf_andviet",
"gulfnotvietnam",
"gulf_and_vietnam",
"vietnam_only",
"viet_and_korea",
"vietkoreawwii",
"koreaonly",
"koreawwii_noviet",
"wwiionly",
"betweengulfandviet",
"bw_korea_and_viet",
"bw_wwii_and_korea",
"prewwii_only")
# Let's add up the major wars (yes, again)
all_places_vets_by_war2 <- all_places_vets_by_war %>% mutate(wwii_service_any_combo = (wwiionly + koreawwii_noviet + vietkoreawwii), korea_service_any_combo = (koreawwii_noviet + koreaonly + vietkoreawwii + viet_and_korea), vietnam_service_any_combo = (vietkoreawwii + viet_and_korea + vietnam_only + gulf_and_vietnam + post911andgulf_andviet), gulf_service_any_combo = (gulf_and_vietnam + gulfnotvietnam + post911andgulf_andviet + post911andgulf_noviet), post_911_any_combo = (post911andgulf_andviet + post911andgulf_noviet + post_911_service_only))
all_places_vets_by_war_totals <- all_places_vets_by_war2
all_places_vets_by_war_pcts <- all_places_vets_by_war2 %>% mutate(across(4:23, ~ (.x/all_places_vets_by_war2$total_population_vet_and_nonvet))*100)
my_state_places_vets_by_war_pcts <- all_places_vets_by_war_pcts %>% filter(str_detect(name, my_state_regexp_string)) %>% print()
all_places_vets_by_war_rank <- all_places_vets_by_war_pcts %>% mutate(across(4:23, ~ rank(-.x, na.last = TRUE, ties.method = c("min")))) %>% print()
my_state_places_vets_by_war_rank <- all_places_vets_by_war_rank %>% filter(str_detect(name, my_state_regexp_string)) %>% select(-geoid, -total_population_vet_and_nonvet)%>% print()
```
This script also includes data on period of wartime service by place (generally a city or town). Because there are so many places in many states, a and more than a dozen wartime service categories in the data, those aren't included in this summary. But the script can be used to crunch and view that data.
```{r vet_status_by_disability_national, eval=TRUE, echo=FALSE, include=FALSE}
# Now, let's move on to another table on a different element of veterans data: Veteran status by disability. This will be table C21007.
# Our variables will start at C21007_001 and count up through _031.
us_vets_by_disab_stat <- get_acs(geography = "us", year = our_year, survey = "acs5", variables = c("C21007_001", "C21007_002", "C21007_003", "C21007_004", "C21007_005", "C21007_006", "C21007_007", "C21007_008", "C21007_009", "C21007_010", "C21007_011", "C21007_012", "C21007_013", "C21007_014", "C21007_015", "C21007_016", "C21007_017", "C21007_018", "C21007_019", "C21007_020", "C21007_021", "C21007_022", "C21007_023", "C21007_024", "C21007_025", "C21007_026", "C21007_027", "C21007_028", "C21007_029", "C21007_030", "C21007_031"), geometry = FALSE) %>% select(-moe) %>% print()
disab_stat_var <- v19 %>% filter(str_detect(concept, "VETERAN")&str_detect(name, "C21007")) %>% print()
us_vets_by_disab_stat2 <- merge(x = us_vets_by_disab_stat, y=disab_stat_var, by.x = "variable", by.y = "name", all = FALSE) %>% print()
us_vets_by_disab_stat3 <- us_vets_by_disab_stat2 %>% select(-concept, -variable) %>% pivot_wider(names_from=label, values_from = estimate, names_sep="_") %>% print()
us_vets_by_disab_stat4 <- janitor::clean_names(us_vets_by_disab_stat3) %>% select(-geoid) %>% print()
us_vets_by_disab_stat_colnames <- colnames(us_vets_by_disab_stat4)
colnames(us_vets_by_disab_stat4) <- gsub("estimate", "Est.", colnames(us_vets_by_disab_stat4))
colnames(us_vets_by_disab_stat4) <- gsub("18_to_64_years", "18_to_64", colnames(us_vets_by_disab_stat4)) %>% print()
colnames(us_vets_by_disab_stat4) <- gsub("in_the_past_12_months", "in_past_12_mos", colnames(us_vets_by_disab_stat4)) %>% print()
colnames(us_vets_by_disab_stat4) <- gsub("with_a_disability", "w_disability", colnames(us_vets_by_disab_stat4)) %>% print()
colnames(us_vets_by_disab_stat4) <- gsub("no_disability", "no_disability", colnames(us_vets_by_disab_stat4)) %>% print()
colnames(us_vets_by_disab_stat4) <- gsub("65_years_and_over", "65_and_over", colnames(us_vets_by_disab_stat4)) %>% print()
us_vets_by_disab_stat_totals <- us_vets_by_disab_stat4 %>% print()
colnames(us_vets_by_disab_stat_totals) <- gsub("_", " ", colnames(us_vets_by_disab_stat_totals)) %>% print()
us_vets_by_disab_stat_totals %>% select(-name) %>% print()
t(us_vets_by_disab_stat_totals %>% select(-name)) %>% print()
us_vets_by_disab_stat5 <- us_vets_by_disab_stat4 %>% mutate(across(4:17, ~ (.x/us_vets_by_disab_stat4$Est._total_18_to_64)*100)) %>% print()
us_vets_by_disab_stat6 <- us_vets_by_disab_stat5 %>% mutate(across(19:32, ~ (.x/us_vets_by_disab_stat5$Est._total_65_and_over)*100)) %>% print()
us_vets_by_disab_stat8 <- us_vets_by_disab_stat6
names(us_vets_by_disab_stat8)[c(-1:-3,-18)] <- gsub("Est._total", "Pct.", names(us_vets_by_disab_stat8)[c(-1:-3,-18)]) %>% print()
us_vets_by_disab_stat_pcts <- us_vets_by_disab_stat8 %>% select(-name) %>% print()
colnames(us_vets_by_disab_stat_pcts) <- gsub("_", " ", colnames(us_vets_by_disab_stat_pcts)) %>% print()
transpose_us_vets_by_disab_stat_pcts1 <- t(us_vets_by_disab_stat_pcts) %>% print()
transpose_us_vets_by_disab_stat_pcts2 <- data.frame(transpose_us_vets_by_disab_stat_pcts1)
transpose_us_vets_by_disab_stat_pcts2$Item <- rownames(transpose_us_vets_by_disab_stat_pcts2) %>% print()
rownames(transpose_us_vets_by_disab_stat_pcts2) <- NULL
transpose_us_vets_by_disab_stat_pcts2 <- transpose_us_vets_by_disab_stat_pcts2[,c(2:1)] %>% print()
colnames(transpose_us_vets_by_disab_stat_pcts2) <- c("Item", "Value") %>% print()
transpose_us_vets_by_disab_stat_pcts <- transpose_us_vets_by_disab_stat_pcts2
transpose_us_vets_by_disab_stat_pcts %>% filter(!str_detect(Item, "Est.")) %>% print()
```
## Veterans by disability status
### In the U.S.: Totals
The Census Bureau provides estimated totals for vets 18 to 64 and those 65 and older; it further breaks each age group down into those who have been below, or at or above, the poverty level in the past 12 months. It further breaks down those subgroups by vets with and without disability. This data is from the 2019 5-year ACS.
Note that the estimated total here, and denominator by which all these percentages was calculated, is not the total U.S. population but is the total U.S. civilian population 18 years and over for whom poverty status is determined. That's about 244 million people.
`r kable(t(us_vets_by_disab_stat_totals %>% select(-name)))`
### In the U.S.: Percentages
Each subgroup here is shown as a percentage of their larger age group.
`r kable(transpose_us_vets_by_disab_stat_pcts, col.names=NULL)`
```{r veterans_by_disability_status_states, eval=TRUE, echo=FALSE, include=FALSE}
# Now, to disability status data, carved up by state.
all_states_vets_by_disab_stat <- get_acs(geography = "state", year = our_year, survey = "acs5", variables = c("C21007_001", "C21007_002", "C21007_003", "C21007_004", "C21007_005", "C21007_006", "C21007_007", "C21007_008", "C21007_009", "C21007_010", "C21007_011", "C21007_012", "C21007_013", "C21007_014", "C21007_015", "C21007_016", "C21007_017", "C21007_018", "C21007_019", "C21007_020", "C21007_021", "C21007_022", "C21007_023", "C21007_024", "C21007_025", "C21007_026", "C21007_027", "C21007_028", "C21007_029", "C21007_030", "C21007_031"), geometry = FALSE) %>% select(-moe) %>% print()
all_states_vets_by_disab_stat2 <- merge(x = all_states_vets_by_disab_stat, y=disab_stat_var, by.x = "variable", by.y = "name", all = FALSE) %>% print()
all_states_vets_by_disab_stat3 <- all_states_vets_by_disab_stat2 %>% select(-concept, -variable) %>% pivot_wider(names_from=label, values_from = estimate, names_sep="_") %>% print()
all_states_vets_by_disab_stat4 <- janitor::clean_names(all_states_vets_by_disab_stat3) %>% select(-geoid) %>% print()
colnames(all_states_vets_by_disab_stat4) <- gsub("estimate", "Est.", colnames(all_states_vets_by_disab_stat4))
colnames(all_states_vets_by_disab_stat4) <- gsub("18_to_64_years", "18 to 64", colnames(all_states_vets_by_disab_stat4)) %>% print()
colnames(all_states_vets_by_disab_stat4) <- gsub("in_the_past_12_months", "in past 12 mos", colnames(all_states_vets_by_disab_stat4)) %>% print()
colnames(all_states_vets_by_disab_stat4) <- gsub("with_a_disability", "w disability", colnames(all_states_vets_by_disab_stat4)) %>% print()
colnames(all_states_vets_by_disab_stat4) <- gsub("no_disability", "no disability", colnames(all_states_vets_by_disab_stat4)) %>% print()
colnames(all_states_vets_by_disab_stat4) <- gsub("65_years_and_over", "65 and over", colnames(all_states_vets_by_disab_stat4)) %>% print()
colnames(all_states_vets_by_disab_stat4) <- gsub("_", " ", colnames(all_states_vets_by_disab_stat4)) %>% print()
all_states_vets_by_disab_stat_totals <- all_states_vets_by_disab_stat4 %>% print()
my_state_vets_by_disab_stat_totals <- all_states_vets_by_disab_stat_totals %>% filter(str_detect(name, my_state_regexp_string)) %>% select(-name) %>% print()
all_states_vets_by_disab_stat5 <- all_states_vets_by_disab_stat4 %>% mutate(across(4:17, ~ (.x/all_states_vets_by_disab_stat4$`Est. total 18 to 64`)*100))
all_states_vets_by_disab_stat6 <- all_states_vets_by_disab_stat5 %>% mutate(across(19:32, ~ (.x/all_states_vets_by_disab_stat5$`Est. total 65 and over`)*100))
all_states_vets_by_disab_stat8 <- all_states_vets_by_disab_stat6
names(all_states_vets_by_disab_stat8)[c(-1:-3,-18)] <- gsub("Est. total", "Pct.", names(all_states_vets_by_disab_stat8)[c(-1:-3,-18)]) %>% print()
my_state_vets_by_disab_stat_pcts <- all_states_vets_by_disab_stat8 %>% filter(str_detect(name, my_state_regexp_string)) %>% select(-name) %>% print()
my_state_vets_by_disab_stat_rank <- all_states_vets_by_disab_stat8 %>% mutate(across(c(4:17,19:32), ~ rank(-.x, na.last = TRUE, ties.method = c("min")))) %>% filter(str_detect(name, my_state_regexp_string)) %>% print()
names(my_state_vets_by_disab_stat_rank)[c(-1:-3,-18)] <- gsub("Pct.", "Rank nationally", names(my_state_vets_by_disab_stat_rank)[c(-1:-3,-18)]) %>% print()
```
### In `r my_state`: Raw totals
The Census Bureau provides estimated totals for vets 18-to-64 and those 65 and older; it further breaks each age group down into those who have been below, or at or above, the poverty level in the past 12 months. It further breaks down those subgroups by vets with and without disability. This data is from the 2019 5-year ACS.
Note that the estimated total here, and denominator by which all these percentages was calculated, is not the total U.S. population but is the total `r my_state` civilian population 18 years and over for whom poverty status is determined. That's about 7.6 million people.
`r kable(t(my_state_vets_by_disab_stat_totals))`
### In `r my_state`: Percentages
Each subgroup here is shown as a percentage of their larger age group.
`r kable(t(my_state_vets_by_disab_stat_pcts))`
### `r my_state`'s rank among the 50 states
`r kable(t(my_state_vets_by_disab_stat_rank %>% select(-name)))`
```{r veterans_by_disability_status_county, eval=TRUE, echo=FALSE, include=FALSE}
all_county_vets_by_disab_stat <- get_acs(geography = "county", year = our_year, survey = "acs5", variables = c("C21007_001", "C21007_002", "C21007_003", "C21007_004", "C21007_005", "C21007_006", "C21007_007", "C21007_008", "C21007_009", "C21007_010", "C21007_011", "C21007_012", "C21007_013", "C21007_014", "C21007_015", "C21007_016", "C21007_017", "C21007_018", "C21007_019", "C21007_020", "C21007_021", "C21007_022", "C21007_023", "C21007_024", "C21007_025", "C21007_026", "C21007_027", "C21007_028", "C21007_029", "C21007_030", "C21007_031"), geometry = FALSE) %>% select(-moe) %>% print()
all_county_vets_by_disab_stat2 <- merge(x = all_county_vets_by_disab_stat, y=disab_stat_var, by.x = "variable", by.y = "name", all = FALSE) %>% print()
all_county_vets_by_disab_stat3 <- all_county_vets_by_disab_stat2 %>% select(-concept, -variable) %>% pivot_wider(names_from=label, values_from = estimate, names_sep="_") %>% print()
all_county_vets_by_disab_stat4 <- janitor::clean_names(all_county_vets_by_disab_stat3) %>% select(-geoid) %>% print()
colnames(all_county_vets_by_disab_stat4) <- gsub("estimate", "Est.", colnames(all_county_vets_by_disab_stat4))
colnames(all_county_vets_by_disab_stat4) <- gsub("18_to_64_years", "18 to 64", colnames(all_county_vets_by_disab_stat4)) %>% print()
colnames(all_county_vets_by_disab_stat4) <- gsub("in_the_past_12_months", "in past 12 mos", colnames(all_county_vets_by_disab_stat4)) %>% print()
colnames(all_county_vets_by_disab_stat4) <- gsub("with_a_disability", "w disability", colnames(all_county_vets_by_disab_stat4)) %>% print()
colnames(all_county_vets_by_disab_stat4) <- gsub("no_disability", "no disability", colnames(all_county_vets_by_disab_stat4)) %>% print()
colnames(all_county_vets_by_disab_stat4) <- gsub("65_years_and_over", "65 and over", colnames(all_county_vets_by_disab_stat4)) %>% print()
all_county_vets_by_disab_stat_totals <- all_county_vets_by_disab_stat4 %>% print()
my_state_county_vets_by_disab_stat_totals <- all_county_vets_by_disab_stat_totals %>% filter(str_detect(name, my_state_regexp_string)) %>% print()
all_county_vets_by_disab_stat5 <- all_county_vets_by_disab_stat4 %>% mutate(across(5:10, ~ (.x/all_county_vets_by_disab_stat4$`Est._total_18 to 64_veteran`)*100))
all_county_vets_by_disab_stat6 <- all_county_vets_by_disab_stat5 %>% mutate(across(12:17, ~ (.x/all_county_vets_by_disab_stat5$`Est._total_18 to 64_nonveteran`)*100))
all_county_vets_by_disab_stat7 <- all_county_vets_by_disab_stat6 %>% mutate(across(20:25, ~ (.x/all_county_vets_by_disab_stat6$`Est._total_65 and over_veteran`)*100))
all_county_vets_by_disab_stat8 <- all_county_vets_by_disab_stat7 %>% mutate(across(27:32, ~ (.x/all_county_vets_by_disab_stat7$`Est._total_65 and over_nonveteran`)*100))
names(all_county_vets_by_disab_stat8)[c(-1:-4,-11,-18:-19,-26)] <- gsub("Est._total", "Pct.", names(all_county_vets_by_disab_stat8)[c(-1:-4,-11,-18:-19,-26)]) %>% print()
colnames(all_county_vets_by_disab_stat8) <- gsub("_", " ", colnames(all_county_vets_by_disab_stat8)) %>% print()
my_state_county_vets_by_disab_stat_pcts <- all_county_vets_by_disab_stat8 %>% filter(str_detect(name, my_state_regexp_string)) %>% print()
colnames(my_state_county_vets_by_disab_stat_pcts) <- gsub("_", " ", colnames(my_state_county_vets_by_disab_stat_pcts)) %>% print()
my_state_vets_by_disab_stat_rank <- all_county_vets_by_disab_stat8 %>% mutate(across(c(5:10,12:17,20:25,27:32), ~ rank(-.x, na.last = TRUE, ties.method = c("min")))) %>% filter(str_detect(name, my_state_regexp_string)) %>% print()
names(my_state_vets_by_disab_stat_rank)[c(-1:-4,-11,-18:-19,-26)] <- gsub("Pct.", "Rank nationally", names(my_state_vets_by_disab_stat_rank)[c(-1:-4,-11,-18:-19,-26)]) %>% print()
```
```{r veterans_by_disability_status_place, eval=TRUE, echo=FALSE, include=FALSE}
# TO_DO: NEED TO GO THROUGH THIS WHOLE SECTION.
all_place_vets_by_disab_stat <- get_acs(geography = "place", year = our_year, survey = "acs5", variables = c("C21007_001", "C21007_002", "C21007_003", "C21007_004", "C21007_005", "C21007_006", "C21007_007", "C21007_008", "C21007_009", "C21007_010", "C21007_011", "C21007_012", "C21007_013", "C21007_014", "C21007_015", "C21007_016", "C21007_017", "C21007_018", "C21007_019", "C21007_020", "C21007_021", "C21007_022", "C21007_023", "C21007_024", "C21007_025", "C21007_026", "C21007_027", "C21007_028", "C21007_029", "C21007_030", "C21007_031"), geometry = FALSE) %>% select(-moe) %>% print()
all_place_vets_by_disab_stat2 <- merge(x = all_place_vets_by_disab_stat, y=disab_stat_var, by.x = "variable", by.y = "name", all = FALSE) %>% print()
all_place_vets_by_disab_stat3 <- all_place_vets_by_disab_stat2 %>% select(-concept, -variable) %>% pivot_wider(names_from=label, values_from = estimate, names_sep="_") %>% print()
all_place_vets_by_disab_stat4 <- janitor::clean_names(all_place_vets_by_disab_stat3) %>% select(-geoid) %>% print()
colnames(all_place_vets_by_disab_stat4) <- gsub("estimate", "est", colnames(all_place_vets_by_disab_stat4))
colnames(all_place_vets_by_disab_stat4) <- gsub("total", "tot", colnames(all_place_vets_by_disab_stat4))
colnames(all_place_vets_by_disab_stat4) <- gsub("18_to_64_years", "18t64", colnames(all_place_vets_by_disab_stat4)) %>% print()
colnames(all_place_vets_by_disab_stat4) <- gsub("nonveteran", "nonvet", colnames(all_place_vets_by_disab_stat4)) %>% print()
colnames(all_place_vets_by_disab_stat4) <- gsub("veteran", "vet", colnames(all_place_vets_by_disab_stat4)) %>% print()
colnames(all_place_vets_by_disab_stat4) <- gsub("income", "inc", colnames(all_place_vets_by_disab_stat4)) %>% print()
colnames(all_place_vets_by_disab_stat4) <- gsub("in_the_past_12_months", "past12mo", colnames(all_place_vets_by_disab_stat4)) %>% print()
colnames(all_place_vets_by_disab_stat4) <- gsub("poverty_level", "pov", colnames(all_place_vets_by_disab_stat4)) %>% print()
colnames(all_place_vets_by_disab_stat4) <- gsub("with_a_disability", "w_dis", colnames(all_place_vets_by_disab_stat4)) %>% print()
colnames(all_place_vets_by_disab_stat4) <- gsub("no_disability", "no_dis", colnames(all_place_vets_by_disab_stat4)) %>% print()
colnames(all_place_vets_by_disab_stat4) <- gsub("65_years_and_over", "65nover", colnames(all_place_vets_by_disab_stat4)) %>% print()
all_place_vets_by_disab_stat5 <- all_place_vets_by_disab_stat4 %>% mutate(across(5:10, ~ (.x/all_place_vets_by_disab_stat4$est_tot_18t64_vet)*100))
all_place_vets_by_disab_stat6 <- all_place_vets_by_disab_stat5 %>% mutate(across(12:17, ~ (.x/all_place_vets_by_disab_stat5$est_tot_18t64_nonvet)*100))
all_place_vets_by_disab_stat7 <- all_place_vets_by_disab_stat6 %>% mutate(across(20:25, ~ (.x/all_place_vets_by_disab_stat6$est_tot_65nover_vet)*100))
all_place_vets_by_disab_stat8 <- all_place_vets_by_disab_stat7 %>% mutate(across(27:32, ~ (.x/all_place_vets_by_disab_stat7$est_tot_65nover_nonvet)*100))
names(all_place_vets_by_disab_stat8)[c(-1:-4,-11,-18:-19,-26)] <- gsub("est_tot", "pct", names(all_place_vets_by_disab_stat8)[c(-1:-4,-11,-18:-19,-26)]) %>% print()
my_state_place_vets_by_disab_stat_pcts <- all_place_vets_by_disab_stat8 %>% filter(str_detect(name, my_state_regexp_string)) %>% print()
my_state_place_vets_by_disab_stat_rank <- all_place_vets_by_disab_stat8 %>% mutate(across(c(5:10,12:17,20:25,27:32), ~ rank(-.x, na.last = TRUE, ties.method = c("min")))) %>% filter(str_detect(name, my_state_regexp_string )) %>% print()
```
```{r median_income_by_veteran_status_national, eval=TRUE, echo=FALSE, include=FALSE}
# Let's switch now to an entirely different metric, median income by veteran status. This is ACS Table B21004.
# B21004_001 is the first variable for median income by veteran status. The variables will count up to _007. Let's try something different this time. Instead of typing, or cutting-and-pasting, all those variables, let's make the computer generate the list of variables for us.
our_next_table <- "B21004"
i <- 1
mylist <- c()
while (i<=7) {
mylist <- c(mylist, i)
i <- i+1
}
# You can see your list by printing it with the next line of code.
# mylist
# actually save the list the way we want it
jp_list <- paste(our_next_table, "_", str_pad(mylist, 3, pad="0"), sep="") %>% print()
# Now, let's ping tidycensus again
us_vets_mhhi <- get_acs(geography = "us", year = our_year, survey = "acs5", variables = jp_list, geometry = FALSE) %>% print()
mhhi_var <- v19 %>% filter(str_detect(concept, "VETERAN")&str_detect(name, our_next_table)) %>% print()
us_vets_mhhi2 <- merge(x = us_vets_mhhi, y=mhhi_var, by.x = "variable", by.y = "name", all = FALSE) %>% print()
head(us_vets_mhhi2)
us_vets_mhhi3 <- us_vets_mhhi2 %>% select(-concept, -variable, -moe) %>% pivot_wider(names_from=label, values_from = estimate, names_sep="_") %>% print()
us_vets_mhhi4 <- janitor::clean_names(us_vets_mhhi3) %>% select(-geoid) %>% print()
colnames(us_vets_mhhi4) <- gsub("estimate", "est", colnames(us_vets_mhhi4)) %>% print()
colnames(us_vets_mhhi4) <- gsub("total", "tot", colnames(us_vets_mhhi4)) %>% print()
colnames(us_vets_mhhi4) <- gsub("median_income", "medinc", colnames(us_vets_mhhi4)) %>% print()
colnames(us_vets_mhhi4) <- gsub("nonveteran", "nonvet", colnames(us_vets_mhhi4)) %>% print()
colnames(us_vets_mhhi4) <- gsub("veteran", "vet", colnames(us_vets_mhhi4)) %>% print()
colnames(us_vets_mhhi4) <- gsub("in_the_past_12_months_in_2019_inflation_adjusted_dollars", "in12mos19infladj", colnames(us_vets_mhhi4)) %>% print()
us_vets_mhhi_amounts <- us_vets_mhhi4
my_state_vets_mhhi_amounts <- us_vets_mhhi_amounts %>% filter(str_detect(name, my_state_regexp_string)) %>% print()
us_vets_mhhi5 <- us_vets_mhhi4 %>% mutate(across(3:8, ~ (.x/us_vets_mhhi4$est_medinc_in12mos19infladj_tot)*100))
us_vets_mhhi_pcts <- us_vets_mhhi5
names(us_vets_mhhi_pcts)[c(-1:-2)] <- gsub("est_medinc", "pct", names(us_vets_mhhi_pcts)[c(-1:-2)]) %>% print()
```
```{r median_income_by_veteran_status_state, eval=TRUE, echo=FALSE, include=FALSE}
# Now, let's look at the state-level data for median income by veteran status.
all_states_vets_mhhi <- get_acs(geography = "state", year = our_year, survey = "acs5", variables = jp_list, geometry = FALSE) %>% print()
mhhi_var <- v19 %>% filter(str_detect(concept, "VETERAN") & str_detect(name, our_next_table)) %>% print()
all_states_vets_mhhi2 <- merge(x = all_states_vets_mhhi, y=mhhi_var, by.x = "variable", by.y = "name", all = FALSE) %>% print()
all_states_vets_mhhi3 <- all_states_vets_mhhi2 %>% select(-concept, -variable, -moe) %>% pivot_wider(names_from=label, values_from = estimate, names_sep="_") %>% print()
all_states_vets_mhhi4 <- janitor::clean_names(all_states_vets_mhhi3) %>% select(-geoid) %>% print()
colnames(all_states_vets_mhhi4) <- gsub("estimate", "est", colnames(all_states_vets_mhhi4)) %>% print()
colnames(all_states_vets_mhhi4) <- gsub("total", "tot", colnames(all_states_vets_mhhi4)) %>% print()
colnames(all_states_vets_mhhi4) <- gsub("median_income", "medinc", colnames(all_states_vets_mhhi4)) %>% print()
colnames(all_states_vets_mhhi4) <- gsub("nonveteran", "nonvet", colnames(all_states_vets_mhhi4)) %>% print()
colnames(all_states_vets_mhhi4) <- gsub("veteran", "vet", colnames(all_states_vets_mhhi4)) %>% print()
colnames(all_states_vets_mhhi4) <- gsub("in_the_past_12_months_in_2019_inflation_adjusted_dollars", "in12mos19infladj", colnames(all_states_vets_mhhi4)) %>% print()
all_states_vets_mhhi_amounts <- all_states_vets_mhhi4
my_state_vets_mhhi_amounts <- all_states_vets_mhhi_amounts %>% filter(str_detect(name, my_state_regexp_string)) %>% print()
all_states_vets_mhhi5 <- all_states_vets_mhhi4 %>% mutate(across(3:8, ~ (.x/all_states_vets_mhhi4$est_medinc_in12mos19infladj_tot)*100)) %>% print()
all_states_vets_mhhi_pcts <- all_states_vets_mhhi5
my_state_vets_mhhi_pcts <- all_states_vets_mhhi_pcts %>% filter(str_detect(name, my_state_regexp_string)) %>% print()
all_states_vets_mhhi_rank <- all_states_vets_mhhi5 %>% mutate(across(3:8, ~ rank(-.x, na.last = TRUE, ties.method = c("min")))) %>% print()
my_state_vets_mhhi_rank <- all_states_vets_mhhi_rank %>% filter(str_detect(name, my_state_regexp_string)) %>% print()
```
```{r median_income_by_veteran_status_county, eval=TRUE, echo=FALSE, include=FALSE}
# Now, let's ping tidycensus again ...
all_county_vets_mhhi <- get_acs(geography = "county", year = our_year, survey = "acs5", variables = jp_list, geometry = FALSE) %>% print()
mhhi_var <- v19 %>% filter(str_detect(concept, "VETERAN") & str_detect(name, our_next_table)) %>% print()
all_county_vets_mhhi2 <- merge(x = all_county_vets_mhhi, y=mhhi_var, by.x = "variable", by.y = "name", all = FALSE) %>% print()
all_county_vets_mhhi3 <- all_county_vets_mhhi2 %>% select(-concept, -variable, -moe) %>% pivot_wider(names_from=label, values_from = estimate, names_sep="_") %>% print()
all_county_vets_mhhi4 <- janitor::clean_names(all_county_vets_mhhi3) %>% select(-geoid) %>% print()
colnames(all_county_vets_mhhi4) <- gsub("estimate", "est", colnames(all_county_vets_mhhi4)) %>% print()
colnames(all_county_vets_mhhi4) <- gsub("total", "tot", colnames(all_county_vets_mhhi4)) %>% print()
colnames(all_county_vets_mhhi4) <- gsub("median_income", "medinc", colnames(all_county_vets_mhhi4)) %>% print()
colnames(all_county_vets_mhhi4) <- gsub("nonveteran", "nonvet", colnames(all_county_vets_mhhi4)) %>% print()
colnames(all_county_vets_mhhi4) <- gsub("veteran", "vet", colnames(all_county_vets_mhhi4)) %>% print()
colnames(all_county_vets_mhhi4) <- gsub("in_the_past_12_months_in_2019_inflation_adjusted_dollars", "in12mos19infladj", colnames(all_county_vets_mhhi4)) %>% print()
all_county_vets_mhhi_amounts <- all_county_vets_mhhi4
my_state_vets_mhhi_amounts <- all_county_vets_mhhi_amounts %>% filter(str_detect(name, my_state_regexp_string)) %>% print()
all_county_vets_mhhi5 <- all_county_vets_mhhi4 %>% mutate(across(3:8, ~ (.x/all_county_vets_mhhi4$est_medinc_in12mos19infladj_tot)*100))
all_county_vets_mhhi_pcts <- all_county_vets_mhhi5
my_state_county_vets_mhhi_pcts <- all_county_vets_mhhi_pcts %>% filter(str_detect(name, my_state_regexp_string)) %>% print()
all_county_vets_mhhi_rank <- all_county_vets_mhhi5 %>% mutate(across(3:8, ~ rank(-.x, na.last = TRUE, ties.method = c("min")))) %>% print()
my_state_county_vets_mhhi_rank <- all_county_vets_mhhi_rank %>% filter(str_detect(name, my_state_regexp_string)) %>% print()
```
```{r median_income_by_veteran_status_place, eval=TRUE, echo=FALSE, include=FALSE}
# Now, to ping tidycensus again ...
all_place_vets_mhhi <- get_acs(geography = "place", year = our_year, survey = "acs5", variables = jp_list, geometry = FALSE) %>% print()
mhhi_var <- v19 %>% filter(str_detect(concept, "VETERAN") & str_detect(name, our_next_table)) %>% print()
all_place_vets_mhhi2 <- merge(x = all_place_vets_mhhi, y=mhhi_var, by.x = "variable", by.y = "name", all = FALSE) %>% print()
all_place_vets_mhhi3 <- all_place_vets_mhhi2 %>% select(-concept, -variable, -moe) %>% pivot_wider(names_from=label, values_from = estimate, names_sep="_") %>% print()
all_place_vets_mhhi4 <- janitor::clean_names(all_place_vets_mhhi3) %>% select(-geoid) %>% print()
colnames(all_place_vets_mhhi4) <- gsub("estimate", "est", colnames(all_place_vets_mhhi4)) %>% print()
colnames(all_place_vets_mhhi4) <- gsub("total", "tot", colnames(all_place_vets_mhhi4)) %>% print()
colnames(all_place_vets_mhhi4) <- gsub("median_income", "medinc", colnames(all_place_vets_mhhi4)) %>% print()
colnames(all_place_vets_mhhi4) <- gsub("nonveteran", "nonvet", colnames(all_place_vets_mhhi4)) %>% print()
colnames(all_place_vets_mhhi4) <- gsub("veteran", "vet", colnames(all_place_vets_mhhi4)) %>% print()
colnames(all_place_vets_mhhi4) <- gsub("in_the_past_12_months_in_2019_inflation_adjusted_dollars", "in12mos19infladj", colnames(all_place_vets_mhhi4)) %>% print()
all_place_vets_mhhi_amounts <- all_place_vets_mhhi4