Project 2

Author

Antik Salimullah

US and China Military Expenditure

Introduction

For this project, I wanted to compare military spending between the US and China over time. I used SIPRI data from 1949 to 2025 and cleaned and analyzed it using R. My goal was to see how China’s military spending has grown and how it compares to the US.

Documentation

I pulled the data from https://www.sipri.org/databases/milex?utm, you are allowed to download the data set straight from the website. The original SIPRI Military Expenditure dataset (Constant (2024) US$ worksheet) contains, 193 rows (including regional headings and a blank row), 80 columns (including 77 year columns from 1949–2025).

In terms of transforming from wide to long, I ran into issues with my code after filtering for China and the US because when using “-country” it takes everything that’s not a country and put’s them in to the rows therefore “Notes” and “…2” fell into those rows. I had to specify “cols = ‘1949’:‘2025’” in order to only pull numerical years into rows. From there I got rid of the irrelevant columns. I then chose to round the numbers to two decimals and then check for missing values, which China had 40. I then removed those 40 NA’s from the dataset.

Finally, I created a line graph and a average using ggplot2 and the mean function in R to compare military spending trends over time and better understand China’s growing military expenditure relative to the United States.

Load Required Packages

library(readxl)
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(tidyr)
library(ggplot2)

Import the Data

sipri_wide <- read_excel(
  "SIPRI-Milex-data-1949-2025_v1.2.xlsx",
  sheet = "Constant (2024) US$",
  skip = 5
)
New names:
• `` -> `...2`
head(sipri_wide)
# A tibble: 6 × 80
  Country    ...2  Notes `1949` `1950` `1951` `1952` `1953` `1954` `1955` `1956`
  <chr>      <lgl> <chr> <chr>  <chr>  <chr>  <chr>  <chr>  <chr>  <chr>  <chr> 
1 <NA>       NA    <NA>  <NA>   <NA>   <NA>   <NA>   <NA>   <NA>   <NA>   <NA>  
2 Africa     NA    <NA>  <NA>   <NA>   <NA>   <NA>   <NA>   <NA>   <NA>   <NA>  
3 North Afr… NA    <NA>  <NA>   <NA>   <NA>   <NA>   <NA>   <NA>   <NA>   <NA>  
4 Algeria    NA    §4    xxx    xxx    xxx    xxx    xxx    xxx    xxx    xxx   
5 Libya      NA    द16 xxx    xxx    ...    ...    ...    ...    ...    ...   
6 Morocco    NA    §17   xxx    xxx    xxx    xxx    xxx    xxx    xxx    122.2…
# ℹ 69 more variables: `1957` <chr>, `1958` <chr>, `1959` <chr>, `1960` <chr>,
#   `1961` <chr>, `1962` <chr>, `1963` <chr>, `1964` <chr>, `1965` <chr>,
#   `1966` <chr>, `1967` <chr>, `1968` <chr>, `1969` <chr>, `1970` <chr>,
#   `1971` <chr>, `1972` <chr>, `1973` <chr>, `1974` <chr>, `1975` <chr>,
#   `1976` <chr>, `1977` <chr>, `1978` <chr>, `1979` <chr>, `1980` <chr>,
#   `1981` <chr>, `1982` <chr>, `1983` <chr>, `1984` <chr>, `1985` <chr>,
#   `1986` <chr>, `1987` <chr>, `1988` <chr>, `1989` <chr>, `1990` <chr>, …

Data Cleaning

sipri_clean <- sipri_wide %>%
  filter(Country %in% c("United States of America", "China"))

head(sipri_clean)
# A tibble: 2 × 80
  Country    ...2  Notes `1949` `1950` `1951` `1952` `1953` `1954` `1955` `1956`
  <chr>      <lgl> <chr> <chr>  <chr>  <chr>  <chr>  <chr>  <chr>  <chr>  <chr> 
1 United St… NA    44    18109… 19462… 41642… 58659… 60395… 52110… 49326… 49972…
2 China      NA    55    ...    ...    ...    ...    ...    ...    ...    ...   
# ℹ 69 more variables: `1957` <chr>, `1958` <chr>, `1959` <chr>, `1960` <chr>,
#   `1961` <chr>, `1962` <chr>, `1963` <chr>, `1964` <chr>, `1965` <chr>,
#   `1966` <chr>, `1967` <chr>, `1968` <chr>, `1969` <chr>, `1970` <chr>,
#   `1971` <chr>, `1972` <chr>, `1973` <chr>, `1974` <chr>, `1975` <chr>,
#   `1976` <chr>, `1977` <chr>, `1978` <chr>, `1979` <chr>, `1980` <chr>,
#   `1981` <chr>, `1982` <chr>, `1983` <chr>, `1984` <chr>, `1985` <chr>,
#   `1986` <chr>, `1987` <chr>, `1988` <chr>, `1989` <chr>, `1990` <chr>, …

Transform Data from Wide to Long Format

sipri_long <- sipri_clean %>%
pivot_longer(
  cols = -Country,
  names_to = "Year",
  values_to = "Military_Expenditure"
)

# Ran into some issues with the columns
head(sipri_long)
# A tibble: 6 × 3
  Country                  Year  Military_Expenditure
  <chr>                    <chr> <chr>               
1 United States of America ...2  <NA>                
2 United States of America Notes 44                  
3 United States of America 1949  181095.54595961623  
4 United States of America 1950  194625.01922071318  
5 United States of America 1951  416423.76619009074  
6 United States of America 1952  586599.33539518306  
# Looking at the columns
names(sipri_wide)
 [1] "Country" "...2"    "Notes"   "1949"    "1950"    "1951"    "1952"   
 [8] "1953"    "1954"    "1955"    "1956"    "1957"    "1958"    "1959"   
[15] "1960"    "1961"    "1962"    "1963"    "1964"    "1965"    "1966"   
[22] "1967"    "1968"    "1969"    "1970"    "1971"    "1972"    "1973"   
[29] "1974"    "1975"    "1976"    "1977"    "1978"    "1979"    "1980"   
[36] "1981"    "1982"    "1983"    "1984"    "1985"    "1986"    "1987"   
[43] "1988"    "1989"    "1990"    "1991"    "1992"    "1993"    "1994"   
[50] "1995"    "1996"    "1997"    "1998"    "1999"    "2000"    "2001"   
[57] "2002"    "2003"    "2004"    "2005"    "2006"    "2007"    "2008"   
[64] "2009"    "2010"    "2011"    "2012"    "2013"    "2014"    "2015"   
[71] "2016"    "2017"    "2018"    "2019"    "2020"    "2021"    "2022"   
[78] "2023"    "2024"    "2025"   
# Going for it again after fixing my code
sipri_long <- sipri_clean %>%
  pivot_longer(
    cols = `1949`:`2025`,
    names_to = "Year",
    values_to = "Military_Expenditure"
  )

# Preview the transformed data
head(sipri_long)
# A tibble: 6 × 5
  Country                  ...2  Notes Year  Military_Expenditure
  <chr>                    <lgl> <chr> <chr> <chr>               
1 United States of America NA    44    1949  181095.54595961623  
2 United States of America NA    44    1950  194625.01922071318  
3 United States of America NA    44    1951  416423.76619009074  
4 United States of America NA    44    1952  586599.33539518306  
5 United States of America NA    44    1953  603952.7491342905   
6 United States of America NA    44    1954  521100.55414618237  

Removing Unnecessary Columns and Rounding

We are here to simply compare military spending for both the US and China, the “Notes” and “..2” columns are not relevant therefore will be removed.

sipri_long <- sipri_long %>%
  select(-`...2`, -Notes)

sipri_long <- sipri_long %>%
  mutate(
    Military_Expenditure = round(as.numeric(Military_Expenditure), 2)
  )
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `Military_Expenditure = round(as.numeric(Military_Expenditure),
  2)`.
Caused by warning:
! NAs introduced by coercion
head(sipri_long)
# A tibble: 6 × 3
  Country                  Year  Military_Expenditure
  <chr>                    <chr>                <dbl>
1 United States of America 1949               181096.
2 United States of America 1950               194625.
3 United States of America 1951               416424.
4 United States of America 1952               586599.
5 United States of America 1953               603953.
6 United States of America 1954               521101.

Check for Missing values

China has 40 years of missing values because they were not recording their military spending at the time.

sipri_long %>%
  group_by(Country) %>%
  summarise(
    Missing_Values = sum(is.na(Military_Expenditure))
  )
# A tibble: 2 × 2
  Country                  Missing_Values
  <chr>                             <int>
1 China                                40
2 United States of America              0

Remove Missing Values

sipri_long <- sipri_long %>%
  filter(!is.na(Military_Expenditure))

head(sipri_long)
# A tibble: 6 × 3
  Country                  Year  Military_Expenditure
  <chr>                    <chr>                <dbl>
1 United States of America 1949               181096.
2 United States of America 1950               194625.
3 United States of America 1951               416424.
4 United States of America 1952               586599.
5 United States of America 1953               603953.
6 United States of America 1954               521101.

Military Expenditure Summary

sipri_long %>%
  group_by(Country) %>%
  summarise(
    Average_Expenditure = round(mean(Military_Expenditure), 2)
  )
# A tibble: 2 × 2
  Country                  Average_Expenditure
  <chr>                                  <dbl>
1 China                                126177.
2 United States of America             695115.

Conclusion

The analysis shows that the United States has consistently spent more on its military than China. The US on average has spent 695 billion compared to China’s 126 million. However, China’s military spending has steadily increased over time, reflecting its growing influence as a global power.

US vs China GDP

Introduction

For this project, I will use World Bank GDP data to compare the economic growth of the United States and China. The original dataset contains GDP figures organized by year in wide format, which I will transform into long format using tidyr. I will then analyze GDP trends over time to better understand how China’s economy has grown relative to the United States.

Documentation

I downloaded the GDP dataset from the World Bank’s World Development Indicators database https://data.worldbank.org/indicator/NY.GDP.MKTP.CD?locations=US-CN. The original dataset was in wide format with 2 rows and 70 columns, including yearly GDP data from 1960 to 2025.

I used read_excel() to import the data and skipped the first three rows to correctly identify the column names. I then removed unnecessary columns using select() and transformed the data from wide to long format using pivot_longer(). The final dataset contains 132 rows and 3 columns: Country Name, Year, and GDP.

I checked for missing values and found none. Finally, I used group_by() and summarise() to calculate average GDP for both countries and created a line graph with ggplot2 to compare their economic growth over time.

Load library packages

library(readxl)
library(dplyr)
library(tidyr)
library(ggplot2)

Import the data

# Import World Bank GDP data
gdp_wide <- read_excel("api_gdp_v2.xls", skip = 3)

# Preview the dataset
head(gdp_wide)
# A tibble: 2 × 70
  `Country Name` `Country Code` `Indicator Name`  `Indicator Code`        `1960`
  <chr>          <chr>          <chr>             <chr>                    <dbl>
1 China          CHN            GDP (current US$) NY.GDP.MKTP.CD    59846235025.
2 United States  USA            GDP (current US$) NY.GDP.MKTP.CD   541988586207.
# ℹ 65 more variables: `1961` <dbl>, `1962` <dbl>, `1963` <dbl>, `1964` <dbl>,
#   `1965` <dbl>, `1966` <dbl>, `1967` <dbl>, `1968` <dbl>, `1969` <dbl>,
#   `1970` <dbl>, `1971` <dbl>, `1972` <dbl>, `1973` <dbl>, `1974` <dbl>,
#   `1975` <dbl>, `1976` <dbl>, `1977` <dbl>, `1978` <dbl>, `1979` <dbl>,
#   `1980` <dbl>, `1981` <dbl>, `1982` <dbl>, `1983` <dbl>, `1984` <dbl>,
#   `1985` <dbl>, `1986` <dbl>, `1987` <dbl>, `1988` <dbl>, `1989` <dbl>,
#   `1990` <dbl>, `1991` <dbl>, `1992` <dbl>, `1993` <dbl>, `1994` <dbl>, …
# Check column names and dimensions
names(gdp_wide)
 [1] "Country Name"   "Country Code"   "Indicator Name" "Indicator Code"
 [5] "1960"           "1961"           "1962"           "1963"          
 [9] "1964"           "1965"           "1966"           "1967"          
[13] "1968"           "1969"           "1970"           "1971"          
[17] "1972"           "1973"           "1974"           "1975"          
[21] "1976"           "1977"           "1978"           "1979"          
[25] "1980"           "1981"           "1982"           "1983"          
[29] "1984"           "1985"           "1986"           "1987"          
[33] "1988"           "1989"           "1990"           "1991"          
[37] "1992"           "1993"           "1994"           "1995"          
[41] "1996"           "1997"           "1998"           "1999"          
[45] "2000"           "2001"           "2002"           "2003"          
[49] "2004"           "2005"           "2006"           "2007"          
[53] "2008"           "2009"           "2010"           "2011"          
[57] "2012"           "2013"           "2014"           "2015"          
[61] "2016"           "2017"           "2018"           "2019"          
[65] "2020"           "2021"           "2022"           "2023"          
[69] "2024"           "2025"          
dim(gdp_wide)
[1]  2 70

Data Cleaning

# Keep country names and yearly GDP values
gdp_clean <- gdp_wide %>%
  select(-`Country Code`, -`Indicator Name`, -`Indicator Code`)

# Preview cleaned data
head(gdp_clean)
# A tibble: 2 × 67
  `Country Name`  `1960`  `1961`  `1962`  `1963`  `1964`  `1965`  `1966`  `1967`
  <chr>            <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
1 China          5.98e10 5.02e10 4.73e10 5.08e10 5.98e10 7.06e10 7.69e10 7.30e10
2 United States  5.42e11 5.62e11 6.04e11 6.37e11 6.84e11 7.42e11 8.13e11 8.60e11
# ℹ 58 more variables: `1968` <dbl>, `1969` <dbl>, `1970` <dbl>, `1971` <dbl>,
#   `1972` <dbl>, `1973` <dbl>, `1974` <dbl>, `1975` <dbl>, `1976` <dbl>,
#   `1977` <dbl>, `1978` <dbl>, `1979` <dbl>, `1980` <dbl>, `1981` <dbl>,
#   `1982` <dbl>, `1983` <dbl>, `1984` <dbl>, `1985` <dbl>, `1986` <dbl>,
#   `1987` <dbl>, `1988` <dbl>, `1989` <dbl>, `1990` <dbl>, `1991` <dbl>,
#   `1992` <dbl>, `1993` <dbl>, `1994` <dbl>, `1995` <dbl>, `1996` <dbl>,
#   `1997` <dbl>, `1998` <dbl>, `1999` <dbl>, `2000` <dbl>, `2001` <dbl>, …

Transform Data from Wide to Long Format

# Transform yearly GDP columns into long format
gdp_long <- gdp_clean %>%
  pivot_longer(
    cols = `1960`:`2025`,
    names_to = "Year",
    values_to = "GDP"
  )

# Preview transformed data
head(gdp_long)
# A tibble: 6 × 3
  `Country Name` Year           GDP
  <chr>          <chr>        <dbl>
1 China          1960  59846235025.
2 China          1961  50162299350.
3 China          1962  47310737754.
4 China          1963  50812227919.
5 China          1964  59821862703.
6 China          1965  70565994356.

Check for Missing Values

# Count missing GDP values by country
gdp_long %>%
  group_by(`Country Name`) %>%
  summarise(
    Missing_Values = sum(is.na(GDP))
  )
# A tibble: 2 × 2
  `Country Name` Missing_Values
  <chr>                   <int>
1 China                       0
2 United States               0

Calculate average GDP in trillions

gdp_long %>%
  group_by(`Country Name`) %>%
  summarise(
    Average_GDP_Trillions = round(mean(GDP) / 1e12, 2)
  )
# A tibble: 2 × 2
  `Country Name` Average_GDP_Trillions
  <chr>                          <dbl>
1 China                           3.83
2 United States                   9.2 

GDP Trends Over Time

ggplot(gdp_long, aes(
  x = as.numeric(Year),
  y = GDP / 1e12,
  color = `Country Name`
)) +
  geom_line() +
  labs(
    title = "U.S. vs. China GDP (1960–2025)",
    x = "Year",
    y = "GDP (Trillions of USD)",
    color = "Country"
  )

Conclusion

The analysis shows that although the United States has maintained a larger GDP, China has experienced significant economic growth over the past several decades. China’s rapid expansion demonstrates how much its economy has developed, even though it has not surpassed the United States in total GDP measured in current U.S. dollars.

National Park Water Chemistry (NPS)

Introduction

For this project, I will analyze water chemistry data collected by the National Park Service from 2006 to 2024. The original dataset is in wide format, with different chemical measurements stored in separate columns. I will use tidyr and dplyr to transform the data into long format, clean missing values, and examine how water chemistry varies across monitoring sites and over time.

Documentation

I used the National Park Service’s Water Quality and Quantity Monitoring dataset (https://catalog.data.gov/dataset/water-quality-and-quantity-monitoring-data-package-for-measurements-collected-wi-2006-2024), which was shared by a classmate in Discussion 5A. The original dataset was in wide format with 3,014 rows and 51 columns, containing water chemistry measurements collected at different national parks and monitoring sites. Make sure to download Chemistry_Data_Wide_20250715.csv from the website if you want to access the original file.

I used read.csv() to import the data and select() to keep the park names, monitoring sites, sampling dates, pH, nitrogen, and phosphorus measurements. I then used pivot_longer() to transform the three measurement columns into rows, creating a tidy dataset with 9,042 rows and 5 columns.

I found 2,089 missing values: 409 for nitrogen, 725 for phosphorus, and 955 for pH. I removed these observations using filter() and is.na() because missing measurements cannot contribute to the analysis. I kept the available measurements rather than treating missing values as zero, leaving 6,953 rows for analysis.

Finally, I used group_by() and summarise() to calculate average measurements and ggplot2 to compare average pH across national parks. This helped me identify differences in water chemistry between locations.

Load Required Packages

library(dplyr)
library(tidyr)
library(ggplot2)

Import Data

water_wide <- read.csv("Chemistry_Data_Wide_20250715.csv")

head(water_wide)
  GroupCode                   GroupName UnitCode             UnitName
1      NETN Northeast Temperate Network     ACAD Acadia National Park
2      NETN Northeast Temperate Network     ACAD Acadia National Park
3      NETN Northeast Temperate Network     ACAD Acadia National Park
4      NETN Northeast Temperate Network     ACAD Acadia National Park
5      NETN Northeast Temperate Network     ACAD Acadia National Park
6      NETN Northeast Temperate Network     ACAD Acadia National Park
  SubUnitCode SubUnitName SiteCode              SiteName SiteType  EventDate
1        <NA>        <NA>   ACABIN Aunt Betty Pond Inlet   Stream 2006-06-26
2        <NA>        <NA>   ACABIN Aunt Betty Pond Inlet   Stream 2006-08-30
3        <NA>        <NA>   ACABIN Aunt Betty Pond Inlet   Stream 2006-08-30
4        <NA>        <NA>   ACABIN Aunt Betty Pond Inlet   Stream 2008-05-13
5        <NA>        <NA>   ACABIN Aunt Betty Pond Inlet   Stream 2008-08-04
6        <NA>        <NA>   ACABIN Aunt Betty Pond Inlet   Stream 2010-05-04
  EventCode Project QCtype LabCode SampleTime SampleDepth_m SampleType pH_Lab
1       484 NETN_LS    ENV    NWQL   16:30:00           0.1          G     NA
2       460 NETN_LS    ENV    NWQL   12:45:00           0.1          G     NA
3       460 NETN_LS    REP    NWQL   12:45:00           0.1          G     NA
4       638 NETN_LS    ENV   SECRL   14:50:00           0.1          G   6.35
5       601 NETN_LS    ENV   SECRL   14:06:15           0.1          G   6.06
6       105 NETN_LS    ENV   SECRL   14:27:00           0.2          G   6.68
  pH_Lab_Method AppColor_PCU AppColorFlag ANC_ueqL ANCFlag Cl_ueqL ClFlag
1          <NA>           40         <NA>   172.18    <NA>      NA   <NA>
2          <NA>           60         <NA>   172.98    <NA>      NA   <NA>
3          <NA>           60         <NA>   169.26    <NA>      NA   <NA>
4         ANCpH           41         <NA>    81.60    <NA>      NA   <NA>
5         ANCpH           70         <NA>   101.00    <NA>      NA   <NA>
6         ANCpH           51         <NA>   139.00    <NA>      NA   <NA>
  SO4_ueqL SO4Flag NO3_ueqL NO3Flag NO2.NO3_mgL NO2.NO3Flag NO2_mgL    NO2Flag
1       NA    <NA>       NA    <NA>          NA   <MRL 0.06      NA <MRL 0.002
2       NA    <NA>       NA    <NA>          NA  <MRL 0.016      NA <MRL 0.002
3       NA    <NA>       NA    <NA>          NA  <MRL 0.016      NA <MRL 0.002
4       NA    <NA>       NA    <NA>          NA  <MRL 0.005      NA <MRL 0.005
5       NA    <NA>       NA    <NA>          NA  <MRL 0.005      NA <MRL 0.005
6       NA    <NA>       NA    <NA>       0.017        <NA>      NA <MRL 0.005
  TotDissN_mgL TotDissNFlag NH3_mgL      NH3Flag TN_mgL TNFlag TP_ugL
1           NA         <NA>   0.006 E, <MRL 0.01  0.430   <NA>   10.0
2           NA         <NA>   0.015         <NA>  0.470   <NA>   14.0
3           NA         <NA>   0.016         <NA>  0.480   <NA>   12.0
4           NA     <MRL 0.1      NA    <MRL 0.08  0.167   <NA>    6.0
5        0.321         <NA>      NA    <MRL 0.08  0.318   <NA>    9.6
6        0.203         <NA>      NA    <MRL 0.08  0.247   <NA>   13.0
      TPFlag TotDissP_ugL TotDissPFlag PO4_ugL   PO4Flag ChlA_ugL ChlAFlag
1 E, <MRL 20           NA         <NA>      NA    <MRL 6       NA     <NA>
2       <NA>           NA         <NA>      NA    <MRL 6       NA     <NA>
3       <NA>           NA         <NA>     4.0 E, <MRL 6       NA     <NA>
4       <NA>          5.4         <NA>     1.4      <NA>       NA     <NA>
5       <NA>          6.6         <NA>      NA    <MRL 1       NA     <NA>
6       <NA>          6.4         <NA>     1.4      <NA>       NA     <NA>
  DOC_mgL DOCFlag Comments IsEventCUI
1      NA    <NA>     <NA>      FALSE
2      NA    <NA>     <NA>      FALSE
3      NA    <NA>     <NA>      FALSE
4      NA    <NA>     <NA>      FALSE
5      NA    <NA>     <NA>      FALSE
6      NA    <NA>     <NA>      FALSE
names(water_wide)
 [1] "GroupCode"     "GroupName"     "UnitCode"      "UnitName"     
 [5] "SubUnitCode"   "SubUnitName"   "SiteCode"      "SiteName"     
 [9] "SiteType"      "EventDate"     "EventCode"     "Project"      
[13] "QCtype"        "LabCode"       "SampleTime"    "SampleDepth_m"
[17] "SampleType"    "pH_Lab"        "pH_Lab_Method" "AppColor_PCU" 
[21] "AppColorFlag"  "ANC_ueqL"      "ANCFlag"       "Cl_ueqL"      
[25] "ClFlag"        "SO4_ueqL"      "SO4Flag"       "NO3_ueqL"     
[29] "NO3Flag"       "NO2.NO3_mgL"   "NO2.NO3Flag"   "NO2_mgL"      
[33] "NO2Flag"       "TotDissN_mgL"  "TotDissNFlag"  "NH3_mgL"      
[37] "NH3Flag"       "TN_mgL"        "TNFlag"        "TP_ugL"       
[41] "TPFlag"        "TotDissP_ugL"  "TotDissPFlag"  "PO4_ugL"      
[45] "PO4Flag"       "ChlA_ugL"      "ChlAFlag"      "DOC_mgL"      
[49] "DOCFlag"       "Comments"      "IsEventCUI"   
dim(water_wide)
[1] 3014   51

Data Cleaning

water_clean <- water_wide %>%
  select(UnitName, SiteName, EventDate,
         pH_Lab, TN_mgL, TP_ugL)

head(water_clean)
              UnitName              SiteName  EventDate pH_Lab TN_mgL TP_ugL
1 Acadia National Park Aunt Betty Pond Inlet 2006-06-26     NA  0.430   10.0
2 Acadia National Park Aunt Betty Pond Inlet 2006-08-30     NA  0.470   14.0
3 Acadia National Park Aunt Betty Pond Inlet 2006-08-30     NA  0.480   12.0
4 Acadia National Park Aunt Betty Pond Inlet 2008-05-13   6.35  0.167    6.0
5 Acadia National Park Aunt Betty Pond Inlet 2008-08-04   6.06  0.318    9.6
6 Acadia National Park Aunt Betty Pond Inlet 2010-05-04   6.68  0.247   13.0

Transform the Data from Wide to Long Format

water_long <- water_clean %>%
  pivot_longer(
    cols = c(pH_Lab, TN_mgL, TP_ugL),
    names_to = "Measurement",
    values_to = "Value"
  )

head(water_long)
# A tibble: 6 × 5
  UnitName             SiteName              EventDate  Measurement Value
  <chr>                <chr>                 <chr>      <chr>       <dbl>
1 Acadia National Park Aunt Betty Pond Inlet 2006-06-26 pH_Lab      NA   
2 Acadia National Park Aunt Betty Pond Inlet 2006-06-26 TN_mgL       0.43
3 Acadia National Park Aunt Betty Pond Inlet 2006-06-26 TP_ugL      10   
4 Acadia National Park Aunt Betty Pond Inlet 2006-08-30 pH_Lab      NA   
5 Acadia National Park Aunt Betty Pond Inlet 2006-08-30 TN_mgL       0.47
6 Acadia National Park Aunt Betty Pond Inlet 2006-08-30 TP_ugL      14   

Check for Missing Values

water_long %>%
  group_by(Measurement) %>%
  summarise(
    Missing_Values = sum(is.na(Value))
  )
# A tibble: 3 × 2
  Measurement Missing_Values
  <chr>                <int>
1 TN_mgL                 409
2 TP_ugL                 725
3 pH_Lab                 955

Remove Missing Values

water_long <- water_long %>%
  filter(!is.na(Value))

head(water_long)
# A tibble: 6 × 5
  UnitName             SiteName              EventDate  Measurement Value
  <chr>                <chr>                 <chr>      <chr>       <dbl>
1 Acadia National Park Aunt Betty Pond Inlet 2006-06-26 TN_mgL       0.43
2 Acadia National Park Aunt Betty Pond Inlet 2006-06-26 TP_ugL      10   
3 Acadia National Park Aunt Betty Pond Inlet 2006-08-30 TN_mgL       0.47
4 Acadia National Park Aunt Betty Pond Inlet 2006-08-30 TP_ugL      14   
5 Acadia National Park Aunt Betty Pond Inlet 2006-08-30 TN_mgL       0.48
6 Acadia National Park Aunt Betty Pond Inlet 2006-08-30 TP_ugL      12   

Water Chemistry Summary

water_long %>%
  group_by(Measurement) %>%
  summarise(
    Average_Value = round(mean(Value), 2)
  )
# A tibble: 3 × 2
  Measurement Average_Value
  <chr>               <dbl>
1 TN_mgL               0.51
2 TP_ugL              22.9 
3 pH_Lab               6.87

Average pH by National Park

water_ph <- water_long %>%
  filter(Measurement == "pH_Lab") %>%
  group_by(UnitName) %>%
  summarise(
    Average_pH = round(mean(Value), 2)
  )


ggplot(water_ph, aes(
  x = UnitName,
  y = Average_pH
)) +
  geom_col() +
  coord_flip() +
  labs(
    title = "Average pH by National Park",
    x = "National Park",
    y = "Average pH"
  )

Interpretation of Water Chemistry Results

The results show that average water pH varies across the national parks, ranging from approximately 6.4 to 8.1. Saratoga National Historical Park has the highest average pH, while Acadia National Park has the lowest. This suggests that water chemistry differs depending on the location, with some parks having more acidic water than others.

Conclusion

The analysis shows that water chemistry varies across national parks, with average pH ranging from approximately 6.4 to 8.1. These differences highlight the importance of monitoring water quality and understanding how environmental conditions vary between locations.