Add data, setup
#Environment>import dataset>From Test Base>default settings (make sure headers=Y), history>highlight data>click to source (will transfer to )
trash_data<- read.csv("~/Downloads/trash_data_template_2026 - trash_data_template_updated.csv")
library(latexpdf)
#str = structure, will tell you the type of data in each column
str(trash_data)
## 'data.frame': 468 obs. of 5 variables:
## $ item_id : chr "bottlecap" "flavor powder wrapper" "gum pack" "styrofoam chunk" ...
## $ length_mm: num 21.9 93.3 65 34.2 139 ...
## $ width_mm : num 21.9 19.8 74.5 36.3 3 ...
## $ height_mm: num 6 0.4 11 0.9 3 0.05 0.1 1.1 0.05 0.2 ...
## $ weight_g : num 0.59 0.48 9.67 0.14 1.7 0.09 0.3 0.71 0.8 0.43 ...
#summary gives you basic summary stats for each numerical variable
summary(trash_data)
## item_id length_mm width_mm height_mm
## Length:468 Min. : 1.64 Min. : 0.05 Min. : 0.000
## Class :character 1st Qu.: 17.60 1st Qu.: 4.00 1st Qu.: 1.000
## Mode :character Median : 28.10 Median : 9.60 Median : 2.500
## Mean : 41.21 Mean :12.67 Mean : 11.250
## 3rd Qu.: 49.81 3rd Qu.:17.00 3rd Qu.: 8.505
## Max. :478.00 Max. :98.00 Max. :232.800
## NA's :1 NA's :1 NA's :1
## weight_g
## Min. : 0.000
## 1st Qu.: 0.120
## Median : 0.340
## Mean : 1.585
## 3rd Qu.: 0.820
## Max. :131.140
##
# 1) Compute volume (cm^3) and add as a new column
# Volume (mm^3) = length * width * height
# Convert mm^3 -> cm^3 by dividing by 1000
############################################################
trash_data$volume_cm3 <- (trash_data$length_mm *
trash_data$width_mm *
trash_data$height_mm) / 1000
Summary Statistics
# Weight summaries
mean_weight <- mean(trash_data$weight_g, na.rm = TRUE)
median_weight<- median(trash_data$weight_g, na.rm = TRUE)
min_weight <- min(trash_data$weight_g, na.rm = TRUE)
max_weight <- max(trash_data$weight_g, na.rm = TRUE)
# Volume summaries
mean_volume <- mean(trash_data$volume_cm3, na.rm = TRUE)
median_volume<- median(trash_data$volume_cm3,na.rm = TRUE)
min_volume <- min(trash_data$volume_cm3, na.rm = TRUE)
max_volume <- max(trash_data$volume_cm3, na.rm = TRUE)
# Print neatly
cat("\n== Weight (g) ==\n",
"Mean:", mean_weight, "\n",
"Median:", median_weight, "\n",
"Min:", min_weight, "\n",
"Max:", max_weight, "\n")
##
## == Weight (g) ==
## Mean: 1.585462
## Median: 0.34
## Min: 0
## Max: 131.14
cat("\n== Volume (cm^3) ==\n",
"Mean:", mean_volume, "\n",
"Median:", median_volume, "\n",
"Min:", min_volume, "\n",
"Max:", max_volume, "\n")
##
## == Volume (cm^3) ==
## Mean: 6.44093
## Median: 0.561824
## Min: 0
## Max: 1258.027
Trash size assessment
############################################################
# 3) Basic frequency plots (histograms) for weight & volume
############################################################
# Histogram for weights
hist(trash_data$weight_g,
main = "Frequency of Trash Weights",
xlab = "Weight (g)")
# Histogram for volumes
hist(trash_data$volume_cm3,
main = "Frequency of Trash Volumes",
xlab = "Volume (cm^3)")
Total trash amounts
#Totals (sum) for weight and volume
total_weight_g <- sum(trash_data$weight_g, na.rm = TRUE)
total_volume_cm3<- sum(trash_data$volume_cm3, na.rm = TRUE)
cat("\n== Totals ==\n",
"Total weight (g):", total_weight_g, "\n",
"Total volume (cm^3):", total_volume_cm3, "\n")
##
## == Totals ==
## Total weight (g): 741.996
## Total volume (cm^3): 3007.914
Extrapolate, how much trash along the entire Michigan Lakeshore? Entire Great Lakes?
# Extrapolate trash totals from a 740 m transect
# to Michigan (1,660 miles) and Great Lakes (10,500 miles)
############################################################
# Extrapolate trash totals and report in kg, lbs, m^3, yd^3
# -- Totals from your dataset (computed earlier) ----------
if (!exists("total_weight_g")) {
total_weight_g <- sum(trash_data$weight_g, na.rm = TRUE)
}
if (!exists("total_volume_cm3")) {
if (!"volume_cm3" %in% names(trash_data)) {
trash_data$volume_cm3 <- (trash_data$length_mm *
trash_data$width_mm *
trash_data$height_mm) / 1000
}
total_volume_cm3 <- sum(trash_data$volume_cm3, na.rm = TRUE)
}
# -- Constants & conversions ------------------------------
survey_len_m <- 400
mile_to_m <- 1609.344
mi_michigan <- 1660 # Michigan lakeshore length (miles)
mi_greatlakes <- 10500 # Total Great Lakes shoreline (miles)
len_michigan_m <- mi_michigan * mile_to_m
len_greatlakes_m <- mi_greatlakes * mile_to_m
# Conversion helpers
g_to_kg <- function(x) x / 1000
kg_to_lb <- function(x) x * 2.20462
cm3_to_m3 <- function(x) x / 1e6
m3_to_yd3 <- function(x) x * 1.30795
# -- Densities per meter ----------------------------------
weight_per_m_g <- total_weight_g / survey_len_m
volume_per_m_cm3 <- total_volume_cm3 / survey_len_m
# -- Michigan shoreline totals ----------------------------
michigan_weight_kg <- g_to_kg(weight_per_m_g * len_michigan_m)
michigan_weight_lb <- kg_to_lb(michigan_weight_kg)
michigan_volume_m3 <- cm3_to_m3(volume_per_m_cm3 * len_michigan_m)
michigan_volume_yd3 <- m3_to_yd3(michigan_volume_m3)
# -- Great Lakes shoreline totals -------------------------
greatlakes_weight_kg <- g_to_kg(weight_per_m_g * len_greatlakes_m)
greatlakes_weight_lb <- kg_to_lb(greatlakes_weight_kg)
greatlakes_volume_m3 <- cm3_to_m3(volume_per_m_cm3 * len_greatlakes_m)
greatlakes_volume_yd3 <- m3_to_yd3(greatlakes_volume_m3)
# -- Print results ----------------------------------------
cat("\n=== Extrapolated Totals (from 740 m sample) ===\n")
##
## === Extrapolated Totals (from 740 m sample) ===
cat("\n-- Michigan shoreline (1,660 miles) --\n",
"Weight: ", round(michigan_weight_kg, 1), " kg (",
round(michigan_weight_lb, 1), " lbs)\n",
"Volume: ", round(michigan_volume_m3, 1), " m^3 (",
round(michigan_volume_yd3, 1), " yd^3)\n", sep="")
##
## -- Michigan shoreline (1,660 miles) --
## Weight: 4955.6 kg (10925.3 lbs)
## Volume: 20.1 m^3 (26.3 yd^3)
cat("\n-- Great Lakes shoreline (10,500 miles) --\n",
"Weight: ", round(greatlakes_weight_kg, 1), " kg (",
round(greatlakes_weight_lb, 1), " lbs)\n",
"Volume: ", round(greatlakes_volume_m3, 1), " m^3 (",
round(greatlakes_volume_yd3, 1), " yd^3)\n", sep="")
##
## -- Great Lakes shoreline (10,500 miles) --
## Weight: 31345.8 kg (69105.6 lbs)
## Volume: 127.1 m^3 (166.2 yd^3)