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.

  1. Remove all but the “Time”, “Temp.Avg”, “Windspeed”, and “Interval.Precip” columns. Save the result in a new dataframe called irma.
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"
  1. Write a silly if-else statement that does the following:
  1. if a variable temp is above 70 AND the variable water is less than .05, the variable recommendation is set to “Enjoy your Day!”
  2. if a variable temp is above 70, but the water variable is more than .05, then the recommendation is set to “Remember your Umbrella!”
  3. Otherwise, the recommendation variable is set to “Go Back to Bed!”

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!"
  1. Finally, get side by side boxplots of the variable “Windspeed”, broken down by Day, and also colored by Day. Make the graph as nice as you can. Then get a histogram of windspeed, colored by Day. What do the graphs show you?
#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"