This document provides an example of a table inspired by the 1980 U.S. Decennial Census using data from that census. These tables are unique because of complex headers and subtotals. These data are an interesting example of historical Census data and origin classification.
Full code and data are available here:
Data for this table come from the IPUMS NHGIS project table NT12A and NT13A from the 1980 Decennial Census. These data represent Age totals and those of Spanish Origin (Munson et al. 2021).
# Read in data
nhgis <- read_csv("data/nhgis0009_ds104_1980_state.csv")
# Grab only total population age columns
age_total <- nhgis %>%
select(STATE, `Under 5 years` = C7B001, `5-17 years` = C7B002, `18-64 years` = C7B003,
`65 years old and over` = C7B004)
# Grab only spanish origin age columns
age_spanish <- nhgis %>%
select(STATE, `Under 5 years` = C7D001, `5-17 years` = C7D002, `18-64 years` = C7D003,
`65 years old and over` = C7D004)
# Join data and remove District Of Columbia
data <- age_total %>%
inner_join(age_spanish, by = c(STATE = "STATE")) %>%
filter(STATE != "District Of Columbia") %>%
mutate(seq = 2:length(age_total$STATE), Total = `Under 5 years.x` + `5-17 years.x` +
`18-64 years.x` + `65 years old and over.x`) %>%
select(STATE, Total, `Under 5 years.x`, `5-17 years.x`, `18-64 years.x`, `65 years old and over.x`,
`Under 5 years.y`, `5-17 years.y`, `18-64 years.y`, `65 years old and over.y`)
# Create Totals Row
totals <- data %>%
summarize(Total = sum(Total), `Under 5 years.x` = sum(`Under 5 years.x`), `5-17 years.x` = sum(`5-17 years.x`),
`18-64 years.x` = sum(`18-64 years.x`), `65 years old and over.x` = sum(`65 years old and over.x`),
`Under 5 years.y` = sum(`Under 5 years.y`), `5-17 years.y` = sum(`5-17 years.y`),
`18-64 years.y` = sum(`18-64 years.y`), `65 years old and over.y` = sum(`65 years old and over.y`)) %>%
mutate(STATE = "Total", seq = 1) %>%
select(STATE, Total, `Under 5 years.x`, `5-17 years.x`, `18-64 years.x`, `65 years old and over.x`,
`Under 5 years.y`, `5-17 years.y`, `18-64 years.y`, `65 years old and over.y`,
seq)
# Bind totals to state data and drop seq var
data <- bind_rows(totals, data) %>%
select(-seq)
# Create column names with spaces after to sub in to make final table show only
# the age categories.
names = c("State", "Total", "Under 5 years", "5-17 years", "18-64 years", "65 years old and over",
"Under 5 years ", "5-17 years ", "18-64 years ", "65 years old and over ")
colnames(data) <- names
# Create table
table <- data %>%
flextable() %>%
add_header_row(colwidths = c(1, 1, 4, 4), values = c("", "The State", "Non-Spanish",
"Spanish")) %>%
add_header_row(colwidths = c(2, 8), values = c("", "Origin")) %>%
theme_vanilla() %>%
vline(j = c("State", "`Total`", "`65 years old and over`", "`65 years old and over `"),
border = fp_border_default(), part = "all") %>%
bold(j = 2) %>%
bold(i = 1) %>%
add_footer_row(values = "Data: IPUMS NHGIS (Munson et al. 2021)", colwidths = 10) %>%
font(part = "all", fontname = "Calibri")
table
Origin | |||||||||
The State | Non-Spanish | Spanish | |||||||
State | Total | Under 5 years | 5-17 years | 18-64 years | 65 years old and over | Under 5 years | 5-17 years | 18-64 years | 65 years old and over |
Total | 225,907,472 | 16,313,889 | 47,297,580 | 136,820,863 | 25,475,140 | 1,662,023 | 3,962,218 | 8,258,836 | 707,917 |
Alabama | 3,893,888 | 296,412 | 865,836 | 2,291,625 | 440,015 | 3,045 | 8,547 | 18,516 | 3,191 |
Alaska | 401,851 | 38,949 | 91,796 | 259,559 | 11,547 | 1,291 | 2,425 | 5,705 | 86 |
Arizona | 2,718,215 | 213,883 | 577,604 | 1,619,366 | 307,362 | 54,763 | 129,250 | 236,891 | 19,797 |
Arkansas | 2,286,435 | 175,592 | 495,782 | 1,302,584 | 312,477 | 2,063 | 4,848 | 9,501 | 1,492 |
California | 23,667,902 | 1,708,400 | 4,680,558 | 14,864,694 | 2,414,250 | 547,386 | 1,221,775 | 2,595,663 | 179,507 |
Colorado | 2,889,964 | 216,495 | 592,318 | 1,833,826 | 247,325 | 38,730 | 96,686 | 188,120 | 16,181 |
Connecticut | 3,107,576 | 185,188 | 637,731 | 1,919,793 | 364,864 | 15,631 | 38,102 | 66,806 | 3,960 |
Delaware | 594,338 | 41,151 | 125,444 | 368,564 | 59,179 | 1,246 | 2,832 | 5,185 | 398 |
Florida | 9,746,324 | 570,224 | 1,789,412 | 5,699,115 | 1,687,573 | 54,251 | 174,615 | 533,696 | 95,596 |
Georgia | 5,463,105 | 414,935 | 1,231,195 | 3,300,244 | 516,731 | 5,802 | 15,020 | 36,671 | 3,767 |
Hawaii | 964,691 | 77,848 | 197,735 | 612,958 | 76,150 | 9,733 | 20,120 | 38,447 | 2,963 |
Idaho | 943,935 | 93,531 | 213,134 | 543,590 | 93,680 | 5,411 | 10,864 | 19,296 | 1,044 |
Illinois | 11,426,518 | 842,241 | 2,400,796 | 6,921,596 | 1,261,885 | 83,505 | 172,889 | 362,235 | 16,973 |
Indiana | 5,490,224 | 418,764 | 1,199,554 | 3,286,522 | 585,384 | 11,199 | 24,511 | 47,698 | 3,639 |
Iowa | 2,913,808 | 221,628 | 604,245 | 1,700,351 | 387,584 | 3,395 | 7,327 | 13,346 | 1,468 |
Kansas | 2,363,679 | 180,877 | 468,158 | 1,408,381 | 306,263 | 8,504 | 17,486 | 34,732 | 2,617 |
Kentucky | 3,660,777 | 282,731 | 799,999 | 2,168,219 | 409,828 | 2,485 | 6,524 | 15,905 | 2,492 |
Louisiana | 4,205,900 | 361,533 | 968,935 | 2,471,153 | 404,279 | 9,231 | 24,630 | 58,300 | 6,973 |
Maine | 1,124,660 | 78,514 | 242,873 | 662,355 | 140,918 | 579 | 1,357 | 2,767 | 302 |
Maryland | 4,216,975 | 272,274 | 895,256 | 2,653,836 | 395,609 | 5,768 | 15,590 | 40,294 | 3,094 |
Massachusetts | 5,737,037 | 337,215 | 1,153,174 | 3,520,117 | 726,531 | 17,900 | 41,815 | 76,175 | 5,153 |
Michigan | 9,262,078 | 685,113 | 2,066,873 | 5,597,834 | 912,258 | 19,844 | 49,341 | 86,693 | 6,562 |
Minnesota | 4,075,970 | 307,249 | 864,559 | 2,424,598 | 479,564 | 4,514 | 9,333 | 16,778 | 1,498 |
Mississippi | 2,520,638 | 215,279 | 598,918 | 1,417,084 | 289,357 | 2,410 | 6,855 | 13,339 | 2,127 |
Missouri | 4,916,686 | 354,144 | 1,008,339 | 2,906,077 | 648,126 | 5,443 | 13,357 | 29,341 | 3,512 |
Montana | 786,690 | 64,455 | 167,440 | 470,236 | 84,559 | 1,414 | 3,012 | 5,146 | 402 |
Nebraska | 1,569,825 | 122,946 | 324,224 | 916,971 | 205,684 | 3,922 | 8,248 | 14,754 | 1,101 |
Nevada | 800,493 | 56,132 | 159,667 | 518,938 | 65,756 | 5,930 | 13,928 | 32,021 | 2,000 |
New Hampshire | 920,610 | 62,512 | 195,570 | 559,561 | 102,967 | 605 | 1,502 | 3,179 | 301 |
New Jersey | 7,364,823 | 463,289 | 1,527,572 | 4,514,191 | 859,771 | 51,428 | 133,134 | 286,722 | 20,599 |
New Mexico | 1,302,894 | 114,731 | 303,176 | 769,081 | 115,906 | 51,222 | 133,286 | 262,628 | 30,086 |
New York | 17,558,072 | 1,135,925 | 3,551,938 | 10,709,442 | 2,160,767 | 169,747 | 427,701 | 984,890 | 76,962 |
North Carolina | 5,881,766 | 404,076 | 1,253,659 | 3,620,850 | 603,181 | 5,444 | 12,859 | 34,707 | 3,657 |
North Dakota | 652,717 | 54,752 | 136,239 | 381,281 | 80,445 | 662 | 1,088 | 2,055 | 97 |
Ohio | 10,797,630 | 787,150 | 2,307,170 | 6,533,850 | 1,169,460 | 14,302 | 33,637 | 66,015 | 5,929 |
Oklahoma | 3,025,290 | 233,307 | 621,577 | 1,794,280 | 376,126 | 7,708 | 16,125 | 31,246 | 2,340 |
Oregon | 2,633,105 | 198,046 | 525,011 | 1,606,712 | 303,336 | 8,931 | 18,380 | 36,656 | 1,880 |
Pennsylvania | 11,863,895 | 747,458 | 2,375,838 | 7,209,666 | 1,530,933 | 18,138 | 45,040 | 83,269 | 7,514 |
Rhode Island | 947,154 | 56,692 | 186,159 | 577,381 | 126,922 | 2,212 | 5,036 | 11,420 | 1,039 |
South Carolina | 3,121,820 | 238,516 | 703,450 | 1,892,526 | 287,328 | 3,116 | 8,473 | 19,837 | 2,000 |
South Dakota | 690,768 | 58,446 | 147,160 | 394,143 | 91,019 | 621 | 1,205 | 2,066 | 131 |
Tennessee | 4,591,120 | 326,088 | 972,472 | 2,774,972 | 517,588 | 2,945 | 7,987 | 19,955 | 3,190 |
Texas | 14,229,191 | 1,169,061 | 3,137,045 | 8,551,924 | 1,371,161 | 352,359 | 876,494 | 1,606,622 | 150,349 |
Utah | 1,461,037 | 189,962 | 350,143 | 811,712 | 109,220 | 9,200 | 17,819 | 31,291 | 1,992 |
Vermont | 511,456 | 35,998 | 109,320 | 307,972 | 58,166 | 256 | 728 | 2,048 | 272 |
Virginia | 5,346,818 | 360,686 | 1,113,648 | 3,367,180 | 505,304 | 7,442 | 18,389 | 50,383 | 3,654 |
Washington | 4,132,156 | 306,123 | 833,237 | 2,561,234 | 431,562 | 16,729 | 33,694 | 66,136 | 3,457 |
West Virginia | 1,949,644 | 145,583 | 414,053 | 1,152,140 | 237,868 | 1,082 | 3,006 | 7,216 | 1,403 |
Wisconsin | 4,705,767 | 346,940 | 1,010,880 | 2,783,750 | 564,197 | 9,109 | 18,412 | 33,192 | 2,259 |
Wyoming | 469,557 | 44,845 | 100,708 | 286,829 | 37,175 | 3,370 | 6,936 | 13,282 | 911 |
Data: IPUMS NHGIS (Munson et al. 2021) | |||||||||
Steven Manson, Jonathan Schroeder, David Van Riper, Tracy Kugler, and Steven Ruggles. IPUMS National Historical Geographic Information System: Version 16.0 [dataset]. Minneapolis, MN: IPUMS. 2021. http://doi.org/10.18128/D050.V16.0