In this lab, we will continue to use the “irmaweather” dataset that is posted on Moodle. Put “irmaweather” in your working directory, and read it into R with the following command:
irma.temp<-read.csv("C:/Users/ethan/Desktop/irmaweather.csv")
Go ahead and view the dataframe irma.temp so you are aware of the variables that are in the dataset.
irma<-irma.temp[,c(1, 2, 21, 31)]
“Interval.Precip” is the amount of rainfall (in cm) that fell in that interval–which is 1 minute. I’d like to find out how much rain fell on each day.
Go ahead and add a “Day” column to the irma dataframe using the lubridate library and the “Time” column. How many unique days are there? What are they?
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
newVector<-day(irma[,1])
irma["Day"]<- newVector
Use the tapply function to find the total rainfall by day. Which day had the most rainfall? Which had the least?
tapply(X = irma[,4], INDEX = irma[,5], FUN = sum)
## 11 12 13
## 2.74 3.46 0.01
print("the 12th had the most rainfall. the 13th had the least")
## [1] "the 12th had the most rainfall. the 13th had the least"
Test your if-else ladder with the average temperature on each of the three days in irma, as well as the sum of interval.precip on each of the three days of irma.
temps <- c(tapply(X = irma[,2], INDEX = irma[,5], FUN = mean))
rainfalls<-c(tapply(X = irma[,4], INDEX = irma[,5], FUN = sum))
recommendation<-""
for (i in 1:3){
if (temps[i]>70){
recommendation<-"Enjoy Your Day!"
if (rainfalls[i] > .05){
recommendation<-"Remember your Umbrella!"
}else{
recommendation<-"Go Back to Bed!"
}
}else{
recommendation<-"Go Back to Bed!"
}
print(recommendation)
}
## [1] "Go Back to Bed!"
## [1] "Go Back to Bed!"
## [1] "Go Back to Bed!"
#install.packages("ggplot2")
library("ggplot2")
## Warning: package 'ggplot2' was built under R version 4.0.3
irma["Windspeed"]<- irma.temp["Windspeed"]
ggplot(irma, aes(x = Windspeed, y = Day, fill = Day)) + geom_boxplot()
## Warning: Continuous x aesthetic -- did you forget aes(group=...)?
ggplot(irma, aes(x = Windspeed, fill = Day)) + geom_histogram(binwidth = 1)
print("the boxplot shows you windspeed each day pretty clearly. the histogram is hard to read")
## [1] "the boxplot shows you windspeed each day pretty clearly. the histogram is hard to read"