R Markdown

#Data manipulation III

Install and load necessary libraries

if (!requireNamespace(“data.table”, quietly = TRUE)) { install.packages(“data.table”) } if (!requireNamespace(“ggplot2”, quietly = TRUE)) { install.packages(“ggplot2”) } if (!requireNamespace(“rmarkdown”, quietly = TRUE)) { install.packages(“rmarkdown”) }

library(data.table) library(ggplot2) library(rmarkdown)

Set working directory (update this path to your actual working directory)

getwd()

Create a PDF to store the plots

pdf(“Temperature_Report.pdf”, width = 8, height = 6)

Read in the data using fread

temperature_data <- fread(“Temperature.csv”)

Extract all winter observations

winter_data <- temperature_data[Season == “Winter”] print(head(winter_data))

Extract all winter observations for zone NC

winter_nc_data <- winter_data[Area == “NC”] print(head(winter_nc_data))

Select only the columns Area, Season, and Temperature

selected_columns <- temperature_data[, .(Area, Season, Temperature)] print(head(selected_columns))

Select only the columns Area and Temperature but only for winter observations

winter_area_temp <- winter_data[, .(Area, Temperature)] print(head(winter_area_temp))

Find the total number of observations in winter

total_winter_observations <- nrow(winter_data) print(total_winter_observations)

Calculate the mean temperature and mean salinity in winter

mean_winter_temp <- mean(winter_data\(Temperature, na.rm = TRUE) mean_winter_salinity <- mean(winter_data\)Salinity, na.rm = TRUE) print(mean_winter_temp) print(mean_winter_salinity)

Find the number of observations per station in winter

observations_per_station_winter <- winter_data[, .N, by = Station] print(observations_per_station_winter)

Find the number of observations per station per season

observations_per_station_season <- temperature_data[, .N, by = .(Station, Season)] print(observations_per_station_season)

Estimate average temperatures by month

avg_temp_by_month <- temperature_data[, .(avg_temp = mean(Temperature, na.rm = TRUE)), by = Month] print(avg_temp_by_month)

Estimate average temperatures by month by area

avg_temp_by_month_area <- temperature_data[, .(avg_temp = mean(Temperature, na.rm = TRUE)), by = .(Month, Area)] print(avg_temp_by_month_area)

Plot the output of the previous question using ggplot2 using the geom_line() geometry

p <- ggplot(avg_temp_by_month_area, aes(x = Month, y = avg_temp, color = Area)) + geom_line() + labs(title = “Average Temperature by Month and Area”, x = “Month”, y = “Average Temperature (C)”) + theme_minimal() print(p)

Close the PDF device

dev.off()ated the plot.