HW2 STAT540
{r} setwd(“C:/Users/edsit/OneDrive/Documents/STAT540/Stat 540 data/hw2 data/”) getwd() files <- list.files(getwd()) # list all files in current working directory dat <- files[grep(“.dat”,files)] # select only those with extension .dat
columns<- c(“Year”,“Day”,“Sup.”)
dat95<- read.fwf(file=“glsea-temps1995_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat96<- read.fwf(file=”glsea-temps1996_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat96<- dat96[-60,]
dat97<- read.fwf(file=“glsea-temps1997_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat98<- read.fwf(file=”glsea-temps1998_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns)
dat99<- read.fwf(file=“glsea-temps1999_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat00<- read.fwf(file=”glsea-temps2000_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat00<- dat00[-60,] dat01<- read.fwf(file=”glsea-temps2001_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat02<- read.fwf(file=”glsea-temps2002_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat03<- read.fwf(file=”glsea-temps2003_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat04<- read.fwf(file=”glsea-temps2004_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat04<- dat04[-60,]
dat05<- read.fwf(file=“glsea-temps2005_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat06<- read.fwf(file=”glsea-temps2006_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat07<- read.fwf(file=”glsea-temps2007_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat08<- read.fwf(file=”glsea-temps2008_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat08<- dat08[-60,]
dat09<- read.fwf(file=“glsea-temps2009_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat10<- read.fwf(file=”glsea-temps2010_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat11<- read.fwf(file=”glsea-temps2011_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat12<- read.fwf(file=”glsea-temps2012_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat12<- dat12[-60,]
dat13<- read.fwf(file=“glsea-temps2013_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat14<- read.fwf(file=”glsea-temps2014_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns)
dat15<- read.fwf(file=“glsea-temps2015_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat16<- read.fwf(file=”glsea-temps2016_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat16<- dat16[-60,]
dat17<- read.fwf(file=“glsea-temps2017_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat18<- read.fwf(file=”glsea-temps2018_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat19<- read.fwf(file=”glsea-temps2019_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat20<- read.fwf(file=”glsea-temps2020_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat20<- dat20[-60,]
dat21<- read.fwf(file=“glsea-temps2021_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat22<- read.fwf(file=”glsea-temps2022_1024.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat23<- read.fwf(file=”glsea-temps2023_1024_3.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat24<- read.fwf(file=”glsea-temps2024_1024_3.dat”, header=F, sep=“,”, na.strings=c(“.”,““), skip=10, widths=c(5,6,8), #number of spaces between each fixed width column strip.white=T, col.names = columns) dat24<- dat24[-60,]
#install.packages(“grDevices”)
plot(x, dat96$Sup. , type = “l”, ylim = c(0,22),col=“green”, xlab = “Months of the Year”,axes=F, ylab = “Water surface Temperature”, main = “Lake Superior Average GLSEA Surface Water Temperature (1995-2024)”, sub=“The warmest year on August 10 for the period of record (1995-2024) was in 2010 (shown in red) The coldest year on August 10 for the period of record (1995-2024) was in 1996 (shown in green)”) abline(h = seq(0,22, by=2), v =c(1,31, 59, 90, 120, 151, 181, 212, 243,273, 304, 334,365) , col = adjustcolor(“gray”, alpha = 0.6), lty = 1)
lines(x, dat95\(Sup., col = adjustcolor("blue", alpha = 0.1), lty = 1, ) lines(x, dat97\)Sup., col = adjustcolor(“blue”, alpha = 0.1), lty = 1, ) lines(x, dat98\(Sup., col = adjustcolor("blue", alpha = 0.1), lty = 1, ) lines(x, dat99\)Sup., col = adjustcolor(“blue”, alpha = 0.1), lty = 1, ) lines(x, dat00\(Sup., col = adjustcolor("blue", alpha = 0.1), lty = 1, ) lines(x, dat01\)Sup., col = adjustcolor(“blue”, alpha = 0.1), lty = 1, ) lines(x, dat02\(Sup., col = adjustcolor("blue", alpha = 0.1), lty = 1, ) lines(x, dat03\)Sup., col = adjustcolor(“blue”, alpha = 0.1), lty = 1, ) lines(x, dat04\(Sup., col = adjustcolor("blue", alpha = 0.1), lty = 1, ) lines(x, dat05\)Sup., col = adjustcolor(“blue”, alpha = 0.1), lty = 1, ) lines(x, dat06\(Sup., col = adjustcolor("blue", alpha = 0.1), lty = 1, ) lines(x, dat07\)Sup., col = adjustcolor(“blue”, alpha = 0.1), lty = 1, ) lines(x, dat08\(Sup., col = adjustcolor("blue", alpha = 0.1), lty = 1, ) lines(x, dat09\)Sup., col = adjustcolor(“blue”, alpha = 0.1), lty = 1, ) lines(x, dat10\(Sup., col = "red", lty = 1, ) lines(x, dat11\)Sup., col = adjustcolor(“blue”, alpha = 0.1), lty = 1, ) lines(x, dat12\(Sup., col = adjustcolor("blue", alpha = 0.1), lty = 1, ) lines(x, dat13\)Sup., col = adjustcolor(“blue”, alpha = 0.1), lty = 1, ) lines(x, dat14\(Sup., col = adjustcolor("blue", alpha = 0.1), lty = 1, ) lines(x, dat15\)Sup., col = adjustcolor(“blue”, alpha = 0.1), lty = 1, ) lines(x, dat16\(Sup., col = adjustcolor("blue", alpha = 0.1), lty = 1, ) lines(x, dat17\)Sup., col = adjustcolor(“blue”, alpha = 0.1), lty = 1, ) lines(x, dat18\(Sup., col = adjustcolor("blue", alpha = 0.1), lty = 1, ) lines(x, dat19\)Sup., col = adjustcolor(“blue”, alpha = 0.1), lty = 1, ) lines(x, dat20\(Sup., col = adjustcolor("blue", alpha = 0.1), lty = 1, ) lines(x, dat21\)Sup., col = adjustcolor(“blue”, alpha = 0.1), lty = 1, ) lines(x, dat22\(Sup., col = adjustcolor("blue", alpha = 0.1), lty = 1, ) lines(x, dat23\)Sup., col = adjustcolor(“blue”, alpha = 0.1), lty = 1, ) lines(x, dat24$Sup., col = adjustcolor(“blue”, alpha = 0.1), lty = 1, )
av<- numeric(29) for (i in 1:365){ av[i]<- (dat95\(Sup.[i]+dat96\)Sup.[i]+dat97\(Sup.[i]+dat98\)Sup.[i]+dat99\(Sup.[i]+dat00\)Sup.[i]+ dat01\(Sup.[i]+dat02\)Sup.[i]+dat03\(Sup.[i]+dat04\)Sup.[i]+dat05\(Sup.[i]+dat06\)Sup.[i]+dat07\(Sup.[i]+dat08\)Sup.[i]+dat09\(Sup.[i]+ dat10\)Sup.[i]+dat11\(Sup.[i]+dat12\)Sup.[i]+dat13\(Sup.[i]+dat14\)Sup.[i]+dat15\(Sup.[i]+dat16\)Sup.[i]+dat17\(Sup.[i]+dat18\)Sup.[i]+dat19\(Sup.[i]+ dat20\)Sup.[i]+dat21\(Sup.[i]+dat22\)Sup.[i]+dat23\(Sup.[i]+dat24\)Sup.[i])/29 } av lines(x, av, col = “black”, lty = 1, ) legend(“topleft”, title=“Years”,legend = c(“1996”, “2010”,“Other Years”,“Average(1995-2024)”), # Text labels for the legend entries col = c(“green”, “red”,adjustcolor(“blue”, alpha = 0.1), “black”), # Colors corresponding to the legend entries lty = 1, # Line types (if applicable) cex = 0.8, # Text size bty = “o”)
xlabels<-c(“Jan1”,“Jan31”, “Feb28”,“Mar31”,“Apr30”,“May31”,“Jun30”,“Jul31”,“Aug31”,“Sep30”,“Oct31”,“Nov30”,“Dec31”) ylabels<-seq(0,22,by=2) str<-“°C” ylabels <- paste0(ylabels, str) otherlab<-seq(32,72,by=4) str2<-“°F” otherlab <- paste0(otherlab, str2) otherlab seq(32,72,by=4) x<-seq(1,365,by=1) axis(side = 1, at = c(1,31, 59, 90, 120, 151, 181, 212, 243,273, 304, 334,365), labels = xlabels) axis(side = 2, at = seq(0,22, by=2), labels = ylabels)# Custom labels axis(side = 4, at = seq(0,(72-32)/95 , by=20/9), labels = otherlab)# Custom labels (72-32)/95