library(tidyverse)
library(janitor)Analysis
Goals of this notebook
This page is dedicated to exploring the Austin Camp Mabry weather station data. We will attempt to answer the following questions:
- Which days had the most rain, and how much rain are we talking about?
- What are the hottest and coldest days in history?
- Which years had the most days of 100+ temperature?
- Which years had the most total rainfall and how much? Which had the least?
- Which years had the most total snowfall and how much?
- Calculate the “average high temperature” and “average low temperature” for each year.
- Since 1990, how much rain do we typically get in January, February, etc. In other words, what is the average rainfall within each month?
Bonus quests:
- Total number of days within each year that had any snow
- Total number of days within each year below freezing (32 or below)
- Total number of days within each year to never get above freezing (not above 32)
- Total number of days within each year that reached 100 or higher in the month of May
- What is the “average yearly temperature” for each year?
- What is the earliest date in each year with 100+ temperature? Which year had the earliest date?
- What is the latest date (but before July 1) for a freeze each year? Which year had the latest freeze date?
Setup
As usual, we begin with attaching a couple of handy library packages.
Importing the cleaned data
Here, we will import, fill an R object, and glimpse at the cleaned data.
A piece of advice: write one line of code, run it, check the results, repeat.
weather <- read_rds("data_processed/01-weather.rds")
weather |> glimpse()Rows: 31,510
Columns: 8
$ station <chr> "USW00013958", "USW00013958", "USW00013958", "USW00013958", "U…
$ name <chr> "AUSTIN CAMP MABRY, TX US", "AUSTIN CAMP MABRY, TX US", "AUSTI…
$ date <date> 1938-06-01, 1938-06-02, 1938-06-03, 1938-06-04, 1938-06-05, 1…
$ prcp <dbl> 0.00, 0.00, 0.00, 0.40, 0.02, 0.00, 0.00, 0.00, 1.60, 0.01, 0.…
$ snow <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
$ snwd <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
$ tmax <dbl> 91, 94, 94, 90, 94, 92, 95, 92, 87, 90, 92, 91, 91, 91, 89, 89…
$ tmin <dbl> 72, 67, 70, 68, 68, 70, 70, 76, 64, 76, 75, 71, 70, 68, 71, 70…
Creating date columns
First, I will create separate columns for year, month, and day to reuse them in future functions.
The yday column which tells us how many days into a year it is (ex. for Feb. 2, it is 33) will be used in a bonus quest below.
weather_dates <- weather |>
mutate(
yr = year(date),
mo = month(date, label = TRUE),
yday = yday(date)
)
weather_datesDays with most rain
Now, I will find the days with the most rain by selecting necessary columns, arranging them with top values on top, and filtering them to be above 5 inches (an arbitrary value to shorten the list).
weather_dates |>
select(prcp, date) |>
arrange(desc(prcp)) |>
filter(prcp >= 5)Data Takeaway: In Austin, the days with the most rain generally fall between May and November, with a record of 7.6 inches in November 15, 2001. More recently, August 26, 2017 saw 5.1 inches, some of the highest of the decade, during Hurricane Harvey that struck the coast of Texas.
Historically hottest and coldest days
For hottest and coldest days in history, I will use the same methodology as above.
For the hottest day: I cut-off minimum temperature at 75 degrees to get an output with the highest temperatures in both min and max columns. Otherwise, many other days with lower temperatures show up.
weather_dates |>
select(date, tmax, tmin) |>
arrange(desc(tmax)) |>
filter(tmax >= 110, tmin >= 75)Now for the coldest day: cut-off is less than or equal to 33 degrees to eliminate warmer days. The head() function crops our list to only show eight rows to match the hottest days one.
P.S. I couldn’t tell which criteria to use for this analysis (the max or min temp), so I included both in my takeaway for the “editor” to decide.
weather_dates |>
select(date, tmax, tmin) |>
arrange(tmin) |>
filter(tmax <= 33) |>
head(8)Data Takeaway: The hottest day in Austin history was August 28, 2011 with a high of 112 degrees Fahrenheit; the coldest was January 31, 1949 with a minimum recorded temperature of -2 degrees (the day before that on January 30 had the coldest “maximum” temperature, with a high of 22 degrees and low of 4 degrees).
Years with most 100+ degree days
I follow the same GSA method as previous functions, only this time I group by my mutated year column.
weather_dates |>
select(yr, tmax) |>
filter(tmax >= 100) |>
group_by(yr) |>
summarize(appearances = n()) |>
arrange(appearances |> desc())Data Takeaway: The year with the most 100+ degree days in Austin was 2011 (90 days), followed by 2023 (80 days) and 2009 (68 days).
Rain by year
Quest: Which years had the most total rainfall and how much? Which had the least?
This quest requires us to use the summarize(sum()) function where we first group our table by years to find total values within each row. We then arrange accordingly depending on whether we want to find “most” or “least” values in the new column.
After creating a new object, don’t forget to flash it at the bottom.
total_rain <- weather_dates |>
group_by(yr) |>
summarize(total_prcp = sum(prcp)) |>
arrange(desc(total_prcp))
total_rainData Takeaway: The year with the most rainfall in Austin history is 2015 with 60 inches, just ahead of 2004 with a precipitation of 52 inches total. The least amount of rainfall in a year was in 1938 with just 11 inches total.
Snow by year
Quest: Which years had the most total snowfall and how much?
Same logic as above, we group by year and summarize the sum of snowfall values to create a new column.
total_snow <- weather_dates |>
group_by(yr) |>
summarize(total_snow = sum(snow)) |>
arrange(desc(total_snow))
total_snowData Takeaway: Austinites saw the most total snowfall in 1985 with a combined 8.7 inches for the entire year.
Average temperatures
Quest: Calculate the “average high temperature” and “average low temperature” for each year.
It is time to introduce another basic math function to find the average of values: mean(). We will follow the same GSA logic but change the summarize argument.
I filtered the object to show the last five years for relevancy.
avg_temps <- weather_dates |>
group_by(yr) |>
summarize(
avg_tmax = mean(tmax),
avg_tmin = mean(tmin)
)
avg_temps |>
filter(yr >= 2019 & yr <= 2024)Data Takeaway: The range for high averages within the last five years are between 81.4 and 83.6 degrees on average, whereas for low they are between 60.0 and 63.5 degrees.
The highest averages for both high and low temperatures are in 2023 (although the data for 2024 is still incomplete).
Monthly rain since 1990
Quest: Since 1990, how much rain do we typically get in January, February, etc. In other words, what is the average rainfall within each month? Logically, we have work through this in two steps:
- Get the total rain for each year/month, like 2.77 total inches in January 1939. To do that you can group by month and year and then calculate the total rain. You should end up with columns for year, month and total rain.
- Take that result and group by month and summarize to get the mean of the total rain in for each month, so you have an average of all the January’s, etc.
I made sure to filter the columns to only include months after 1990.
monthly_rainfall <- weather_dates |>
filter(yr >= 1990) |>
group_by(mo, yr) |>
summarize(total_rain_mo_yr = sum(prcp))`summarise()` has grouped output by 'mo'. You can override using the `.groups`
argument.
monthly_rainfall |>
group_by(mo) |>
summarize(avg_rain = mean(total_rain_mo_yr))Data Takeaway: Since 1900, May is the month with the most average rainfall of 5 inches in Austin.
Bonus quests
Total number of days within each year that had any snow:
weather_dates |>
select(yr, snow) |>
filter(snow > 0) |>
group_by(yr) |>
summarize(appearances = n()) |>
arrange(appearances |> desc())Data Takeaway: Although the list is not long, this chart shows the total number of snow days for each year in Austin. The year with the greatest amount of snow days was 1973.
Total number of days within each year below freezing (32 or below):
I manually double-checked all 82 rows so that no tmin row had a higher value that tmax (might be impossible, but just in case), so that column can be excluded.
weather_dates |>
select(yr, tmax) |>
filter(tmax <= 32) |>
group_by(yr) |>
summarize(appearances = n()) |>
arrange(appearances |> desc())Data Takeaway: There have been 38 years since 1938 that had days with freezing temperatures of 32 degrees or below in Austin, with 1983 having the most (six days). The most recent year was 2021 when Austin experienced a winter freeze (five days).
Total number of days within each year that reached 100 or higher in the month of May:
weather_dates |>
select(yr, mo, tmax) |>
filter(mo == "May" & tmax >= 100) |>
group_by(yr) |>
summarize(appearances = n()) |>
arrange(appearances |> desc())Data Takeaway: The greatest amount of days with 100 degrees or above in May in Austin is 2011, totaling three. There are only six other years that had such hot days in May.
Average yearly temperature for each year:
The object named “avg_temps” can be reused in this quest. We will go back to using mutate() to create a new column with additional basic math of finding the average of tmax and tmin columns.
I arranged by year for chronology, but you can filter the data based on what you want to see.
avg_yearly_temps <- avg_temps |>
mutate(avg_yearly_temp = (avg_tmax + avg_tmin) / 2) |>
select(yr, avg_yearly_temp) |>
arrange(yr)
avg_yearly_temps |>
filter(yr == 2023)Data Takeaway: Last year, Austin saw a yearly average temperature of 72.3 degrees Fahrenheit.
Earliest date in each year with 100+ temperature:
To find which year had the earliest date, yday will prove useful. I made sure to use summarize(min) to aggregate the earliest days within each year.
weather_dates |>
filter(tmax >= 100) |>
group_by(yr) |>
summarize(earliest_yday = min(yday)) |>
arrange(earliest_yday)Data Takeaway: The year with the earliest 100-plus degree temperature was 1984, that being May 5.
Latest day for a freeze each year
The challenge in this quest was making sure that the latest date is not after July 1. To do this, I will filter the yday to be under 180 days from the start of the year. I realize this isn’t particular to leap years and such, but since the latest day isn’t until 00 anyway, it still works.
weather_dates |>
filter(tmax <= 32, yday <= 180) |>
group_by(yr) |>
summarize(latest_yday = max(yday)) |>
arrange(desc(latest_yday))Data Takeaway: In 1948, Austin saw the latest date when the temperature reached a freezing level on March 12.