# Load necessary libraries
library(readr)
## Warning: package 'readr' was built under R version 4.3.3
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
# Load data from GitHub
data <- read_csv("https://raw.githubusercontent.com/Emin-NYC/congress-age-data/main/data_aging_congress.csv")
## Rows: 29120 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): chamber, state_abbrev, bioname, bioguide_id, generation
## dbl (6): congress, party_code, cmltv_cong, cmltv_chamber, age_days, age_years
## date (2): start_date, birthday
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Create relevant subset of the data
data_subset <- data[, c("congress", "chamber", "age_years", "party_code", "generation")]
# Rename columns
colnames(data_subset) <- c("Congress", "Chamber", "Age", "Party", "Generation")
# Transform columns
data_subset$Party <- ifelse(data_subset$Party == 100, "Democrat",
ifelse(data_subset$Party == 200, "Republican", "Other"))
data_subset$Age <- round(data_subset$Age)
# Calculate average age by Congress
congress_average_age <- data_subset %>%
group_by(Congress) %>%
summarise(Average_Age = mean(Age, na.rm = TRUE))
# Show the first few rows of the result
head(congress_average_age)
## # A tibble: 6 × 2
## Congress Average_Age
## <dbl> <dbl>
## 1 66 51.7
## 2 67 52.6
## 3 68 52.5
## 4 69 53.2
## 5 70 53.9
## 6 71 54.6
# Plot average age over time
ggplot(congress_average_age, aes(x = Congress, y = Average_Age)) +
geom_line(color = "blue") +
geom_point() +
labs(title = "Average Age of Congress over Time", x = "Congress", y = "Average Age") +
theme_minimal()
Our graph results show that the average age of Congress has indeed increased over time, confirming the hypothesis. Future analysis could potentially explore the differences in age between political parties and how that might influence policy making.