Updated by NDG 2/7/24
You have been hired as a consultant by Disney to create a location for a new amusement park. Your job is to analyze weather data from different locations to pick out the best option.
Begin by looking at the documentation for the climate normal data.
Then load your ClimateNormalData.csv datafile. You will need to do some clean-up. Be sure to look at the data carefully. Below are a list of suggested dplyr activities.
Suggested tasks:
#load all Libraries
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)
library(dplyr)
#loading data, skipping shows and keeping a heading
raw_data_temp <- read.csv("ClimateNormalData.csv", skip =3, header = TRUE)
raw_data_station <- read.csv("ClimateNormalData_stations.csv", skip =2, header = TRUE)
#Turn all character date fields into integers
str(raw_data_temp)
## 'data.frame': 35040 obs. of 57 variables:
## $ STATION : chr "USW00014762" "USW00014762" "USW00014762" "USW00014762" ...
## $ month : int 1 1 1 1 1 1 1 1 1 1 ...
## $ day : int 1 1 1 1 1 1 1 1 1 1 ...
## $ hour : int 0 1 2 3 4 5 6 7 8 9 ...
## $ HLY.TEMP.NORMAL : num 31.4 31.1 30.8 30.5 30.1 29.8 29.6 29.4 29.1 29.7 ...
## $ years_HLY.TEMP.NORMAL : int 15 15 15 15 15 15 15 15 15 15 ...
## $ HLY.TEMP.10PCTL : num 14 14 12.9 12.9 12.9 12 10.9 10.9 10.9 12.9 ...
## $ years_HLY.TEMP.10PCTL : int 15 15 15 15 15 15 15 15 15 15 ...
## $ HLY.TEMP.90PCTL : num 48.9 46.9 46 46 46 46 46 46 45 45 ...
## $ years_HLY.TEMP.90PCTL : int 15 15 15 15 15 15 15 15 15 15 ...
## $ HLY.DEWP.NORMAL : num 23.3 23.2 23.2 23.2 23.1 22.9 22.7 22.6 22.5 22.7 ...
## $ years_HLY.DEWP.NORMAL : int 15 15 15 15 15 15 15 15 15 15 ...
## $ HLY.DEWP.10PCTL : num 3 3.9 3.9 3 3.9 3 1.9 1.9 1.9 3 ...
## $ meas_flag_HLY.DEWP.10PCTL: logi NA NA NA NA NA NA ...
## $ years_HLY.DEWP.10PCTL : int 15 15 15 15 15 15 15 15 15 15 ...
## $ HLY.DEWP.90PCTL : num 43 42.1 41 41 42.1 41 41 41 39.9 41 ...
## $ years_HLY.DEWP.90PCTL : int 15 15 15 15 15 15 15 15 15 15 ...
## $ HLY.PRES.NORMAL : num 1019 1019 1019 1019 1019 ...
## $ years_HLY.PRES.NORMAL : int 15 15 15 15 15 15 15 15 14 15 ...
## $ HLY.PRES.10PCTL : num 1009 1009 1008 1008 1008 ...
## $ years_HLY.PRES.10PCTL : int 15 15 15 15 15 15 15 15 14 15 ...
## $ HLY.PRES.90PCTL : num 1030 1030 1029 1030 1030 ...
## $ years_HLY.PRES.90PCTL : int 15 15 15 15 15 15 15 15 14 15 ...
## $ HLY.CLDH.NORMAL : num 0 0 0 0 0 0 0 0 0 0 ...
## $ years_HLY.CLDH.NORMAL : int 15 15 15 15 15 15 15 15 15 15 ...
## $ HLY.HTDH.NORMAL : num 33.6 33.9 34.2 34.5 34.9 35.2 35.4 35.6 35.9 35.3 ...
## $ years_HLY.HTDH.NORMAL : int 15 15 15 15 15 15 15 15 15 15 ...
## $ HLY.CLOD.PCTCLR : num -9999 -9999 -9999 -9999 -9999 ...
## $ years_HLY.CLOD.PCTCLR : int 0 0 0 0 0 0 0 0 0 0 ...
## $ HLY.CLOD.PCTFEW : num -9999 -9999 -9999 -9999 -9999 ...
## $ years_HLY.CLOD.PCTFEW : int 0 0 0 0 0 0 0 0 0 0 ...
## $ HLY.CLOD.PCTSCT : num -9999 -9999 -9999 -9999 -9999 ...
## $ years_HLY.CLOD.PCTSCT : int 0 0 0 0 0 0 0 0 0 0 ...
## $ HLY.CLOD.PCTBKN : num -9999 -9999 -9999 -9999 -9999 ...
## $ years_HLY.CLOD.PCTBKN : int 0 0 0 0 0 0 0 0 0 0 ...
## $ HLY.CLOD.PCTOVC : num -9999 -9999 -9999 -9999 -9999 ...
## $ years_HLY.CLOD.PCTOVC : int 0 0 0 0 0 0 0 0 0 0 ...
## $ HLY.HIDX.NORMAL : num 31.4 31.1 30.8 30.5 30.1 29.8 29.6 29.4 29.1 29.7 ...
## $ years_HLY.HIDX.NORMAL : int 15 15 15 15 15 15 15 15 15 15 ...
## $ HLY.WCHL.NORMAL : num 24.7 24.1 23.8 23.4 23.1 22.4 22.2 21.9 21.6 22.1 ...
## $ years_HLY.WCHL.NORMAL : int 15 15 15 15 15 15 15 15 15 15 ...
## $ HLY.WIND.AVGSPD : num 8.5 8.6 8.5 8.5 8.6 8.8 8.9 8.7 8.7 9.2 ...
## $ years_HLY.WIND.AVGSPD : int 15 15 15 15 15 15 15 15 15 15 ...
## $ HLY.WIND.PCTCLM : num 8 5.4 5.4 5.4 6.7 3.6 3.6 3.1 5.8 7.2 ...
## $ years_HLY.WIND.PCTCLM : int 15 15 15 15 15 15 15 15 15 15 ...
## $ HLY.WIND.VCTDIR : int 237 236 238 238 238 237 237 236 235 237 ...
## $ years_HLY.WIND.VCTDIR : int 15 15 15 15 15 15 15 15 15 15 ...
## $ HLY.WIND.VCTSPD : num 4.8 4.6 4.7 4.5 4.8 4.8 5 5 5.1 5.7 ...
## $ years_HLY.WIND.VCTSPD : int 15 15 15 15 15 15 15 15 15 15 ...
## $ HLY.WIND.1STDIR : int 7 5 7 7 7 7 7 7 7 7 ...
## $ years_HLY.WIND.1STDIR : int 15 15 15 15 15 15 15 15 15 15 ...
## $ HLY.WIND.1STPCT : num 24.1 26.8 25.6 25.7 26.7 26.5 25.5 25.1 23.7 27.4 ...
## $ years_HLY.WIND.1STPCT : int 15 15 15 15 15 15 15 15 15 15 ...
## $ HLY.WIND.2NDDIR : int 5 7 5 5 5 5 5 5 5 5 ...
## $ years_HLY.WIND.2NDDIR : int 15 15 15 15 15 15 15 15 15 15 ...
## $ HLY.WIND.2NDPCT : num 21.1 26 25.2 24.9 22.5 25.3 25.1 23 22.4 25.7 ...
## $ years_HLY.WIND.2NDPCT : int 15 15 15 15 15 15 15 15 15 15 ...
str(raw_data_station)
## 'data.frame': 4 obs. of 5 variables:
## $ STATION : chr "USW00014762" "USW00004725" "USW00013904" "USW00025309"
## $ NAME : chr "PITTSBURGH ALLEGHENY CO AP, PA US" "BINGHAMTON, NY US" "AUSTIN BERGSTROM AP, TX US" "JUNEAU INTL AP, AK US"
## $ LATITUDE : num 40.4 42.2 30.2 58.4
## $ LONGITUDE: num -79.9 -76 -97.7 -134.6
## $ ELEVATION: num 380.4 489.5 146.3 4.9
#the types are correct?
#if they were not this is an example of what the code would look like to change that
#raw_data_temp <- raw_data_temp %>%
#mutate(date_field1 = as.integer(date_field1),
#date_field2 = as.integer(date_field2))
raw_data_temp <- raw_data_temp %>%
mutate(new_date_string = paste0(month, "/", day, "/2024"), #paste0 creates a structure
actual_date_column = mdy(new_date_string)) %>% #mdy tells r the stucture
select(-new_date_string)
head(raw_data_temp) #visual check
#STATION, actual_date_column,HLY.WIND.2NDDIR,HLY.TEMP.NORMAL
#use dply in raw_data_temp to select certain columns and rename them to have _ instead of .
data_temp <- raw_data_temp %>%
select(STATION, actual_date_column, HLY.WIND.AVGSPD, HLY.TEMP.NORMAL,HLY.TEMP.10PCTL,HLY.TEMP.90PCTL) %>%
rename(STATION = STATION,
actual_date_column = actual_date_column,
HLY_WIND_AVGSPD = HLY.WIND.AVGSPD,
HLY_TEMP_NORMAL = HLY.TEMP.NORMAL,
HLY_TEMP_10PCTL = HLY.TEMP.10PCTL,
HLY_TEMP_90PCTL = HLY.TEMP.90PCTL )
#join tables data_temp and raw_data_station on column STATION
result <- left_join(data_temp, raw_data_station, by = "STATION")
Now that you have the data, create some basic summary data. Show a table with the average temperature by month and location. Have the stations as rows, and the month as columns.
Hint: you may need to use dplyr pivot.
*** I did more than asked to help with the following questions
# Extract month and year from the actual_date_column
result2 <- result %>%
mutate(month = format(as.Date(actual_date_column), "%m"),
year = format(as.Date(actual_date_column), "%Y"))
# Create a summary table with average temperature by month and location
summary_table <- result2 %>%
group_by(STATION, month) %>%
summarise(avg_temp = mean(HLY_TEMP_NORMAL, na.rm = TRUE))
## `summarise()` has grouped output by 'STATION'. You can override using the
## `.groups` argument.
print(summary_table)
## # A tibble: 50 × 3
## # Groups: STATION [6]
## STATION month avg_temp
## <chr> <chr> <dbl>
## 1 "" 09 60.6
## 2 "" 12 29.6
## 3 "USW00004725" 01 24.2
## 4 "USW00004725" 02 24.8
## 5 "USW00004725" 03 33.2
## 6 "USW00004725" 04 45.5
## 7 "USW00004725" 05 57.0
## 8 "USW00004725" 06 65.1
## 9 "USW00004725" 07 69.9
## 10 "USW00004725" 08 67.3
## # ℹ 40 more rows
# Pivot the summary table to have stations as rows and months as columns, so we can see by each stattion
summary_table_pivot <- summary_table %>%
pivot_wider(names_from = month, values_from = avg_temp)
#you can add column names for better readability
colnames(summary_table_pivot) <- c("Station", paste("Month", unique(summary_table$month)))
# View the resulting summary table
print(summary_table_pivot)
## # A tibble: 6 × 13
## # Groups: Station [6]
## Station `Month 09` `Month 12` `Month 01` `Month 02` `Month 03` `Month 04`
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 "" 60.6 29.6 NA NA NA NA
## 2 "USW0000472… 60.7 29.5 24.2 24.8 33.2 45.5
## 3 "USW0001390… 79.0 52.1 50.5 54.5 62.5 68.6
## 4 "USW0001476… 65.9 35.2 30.1 NA NA 52.7
## 5 "USW0002530… 50.2 30.4 30.2 29.8 32.6 40.7
## 6 "x" NA NA NA 31.1 41.0 NA
## # ℹ 6 more variables: `Month 05` <dbl>, `Month 06` <dbl>, `Month 07` <dbl>,
## # `Month 08` <dbl>, `Month 10` <dbl>, `Month 11` <dbl>
# Create a summary table with average temperature by month and location
summary_table_10 <- result2 %>%
group_by(STATION, month) %>%
summarise(temp_10p = mean(HLY_TEMP_10PCTL, na.rm = TRUE))
## `summarise()` has grouped output by 'STATION'. You can override using the
## `.groups` argument.
print(summary_table_10)
## # A tibble: 50 × 3
## # Groups: STATION [6]
## STATION month temp_10p
## <chr> <chr> <dbl>
## 1 "" 09 50
## 2 "" 12 16.7
## 3 "USW00004725" 01 8.70
## 4 "USW00004725" 02 10.6
## 5 "USW00004725" 03 19.2
## 6 "USW00004725" 04 32.3
## 7 "USW00004725" 05 45.0
## 8 "USW00004725" 06 56.2
## 9 "USW00004725" 07 62.7
## 10 "USW00004725" 08 60.2
## # ℹ 40 more rows
# Pivot the summary table to have stations as rows and months as columns, so we can see by each stattion
summary_table_pivot_10 <- summary_table_10 %>%
pivot_wider(names_from = month, values_from = temp_10p)
#you can add column names for better readability
colnames(summary_table_pivot_10) <- c("Station", paste("Month", unique(summary_table_10$month)))
# View the resulting summary table
print(summary_table_pivot_10)
## # A tibble: 6 × 13
## # Groups: Station [6]
## Station `Month 09` `Month 12` `Month 01` `Month 02` `Month 03` `Month 04`
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 "" 50 16.7 NA NA NA NA
## 2 "USW0000472… 50.9 16.6 8.70 10.6 19.2 32.3
## 3 "USW0001390… 70.8 37.8 35.9 38.7 49.0 57.3
## 4 "USW0001476… 56.2 21.8 13.4 NA NA 38.2
## 5 "USW0002530… 44.4 17.2 17.5 16.7 22.9 34.3
## 6 "x" NA NA NA 15.5 26.7 NA
## # ℹ 6 more variables: `Month 05` <dbl>, `Month 06` <dbl>, `Month 07` <dbl>,
## # `Month 08` <dbl>, `Month 10` <dbl>, `Month 11` <dbl>
# Create a summary table with average temperature by month and location
summary_table_90 <- result2 %>%
group_by(STATION, month) %>%
summarise(temp_90p = mean(HLY_TEMP_90PCTL, na.rm = TRUE))
## `summarise()` has grouped output by 'STATION'. You can override using the
## `.groups` argument.
print(summary_table_90)
## # A tibble: 50 × 3
## # Groups: STATION [6]
## STATION month temp_90p
## <chr> <chr> <dbl>
## 1 "" 09 69.1
## 2 "" 12 37.9
## 3 "USW00004725" 01 39.0
## 4 "USW00004725" 02 38.4
## 5 "USW00004725" 03 48.9
## 6 "USW00004725" 04 59.2
## 7 "USW00004725" 05 68.8
## 8 "USW00004725" 06 74.0
## 9 "USW00004725" 07 76.8
## 10 "USW00004725" 08 74.2
## # ℹ 40 more rows
# Pivot the summary table to have stations as rows and months as columns, so we can see by each stattion
summary_table_pivot_90 <- summary_table_90 %>%
pivot_wider(names_from = month, values_from = temp_90p)
#you can add column names for better readability
colnames(summary_table_pivot_90) <- c("Station", paste("Month", unique(summary_table_90$month)))
# View the resulting summary table
print(summary_table_pivot_90)
## # A tibble: 6 × 13
## # Groups: Station [6]
## Station `Month 09` `Month 12` `Month 01` `Month 02` `Month 03` `Month 04`
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 "" 69.1 37.9 NA NA NA NA
## 2 "USW0000472… 70.7 43.0 39.0 38.4 48.9 59.2
## 3 "USW0001390… 85.6 68.1 65.4 69.7 73.9 78.0
## 4 "USW0001476… 75.3 50.9 47.2 NA NA 67.0
## 5 "USW0002530… 55.5 39.4 39.5 39.0 40.9 47.2
## 6 "x" NA NA NA 48.3 58.5 NA
## # ℹ 6 more variables: `Month 05` <dbl>, `Month 06` <dbl>, `Month 07` <dbl>,
## # `Month 08` <dbl>, `Month 10` <dbl>, `Month 11` <dbl>
# Extract month and year from the actual_date_column
result2 <- result %>%
mutate(month = format(as.Date(actual_date_column), "%m"),
year = format(as.Date(actual_date_column), "%Y"))
# Create a summary table with average temperature by month and location
summary_table_w <- result2 %>%
group_by(STATION, month) %>%
summarise(avg_wind = mean(HLY_WIND_AVGSPD, na.rm = TRUE))
## `summarise()` has grouped output by 'STATION'. You can override using the
## `.groups` argument.
print(summary_table_w)
## # A tibble: 50 × 3
## # Groups: STATION [6]
## STATION month avg_wind
## <chr> <chr> <dbl>
## 1 "" 09 5.5
## 2 "" 12 7.73
## 3 "USW00004725" 01 9.06
## 4 "USW00004725" 02 9.42
## 5 "USW00004725" 03 9.39
## 6 "USW00004725" 04 9.15
## 7 "USW00004725" 05 7.78
## 8 "USW00004725" 06 7.08
## 9 "USW00004725" 07 6.35
## 10 "USW00004725" 08 6.21
## # ℹ 40 more rows
# Pivot the summary table to have stations as rows and months as columns, so we can see by each stattion
summary_table_pivot_w<- summary_table_w %>%
pivot_wider(names_from = month, values_from = avg_wind)
#you can add column names for better readability
colnames(summary_table_pivot_w) <- c("Station", paste("Month", unique(summary_table_w$month)))
# View the resulting summary table
print(summary_table_pivot_w)
## # A tibble: 6 × 13
## # Groups: Station [6]
## Station `Month 09` `Month 12` `Month 01` `Month 02` `Month 03` `Month 04`
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 "" 5.5 7.73 NA NA NA NA
## 2 "USW0000472… 6.70 8.95 9.06 9.42 9.39 9.15
## 3 "USW0001390… -21.8 7.27 7.58 8.59 8.85 8.78
## 4 "USW0001476… 5.71 8.54 8.97 NA NA 8.48
## 5 "USW0002530… 6.98 7.29 7.38 6.88 7.21 7.58
## 6 "x" NA NA NA 8.99 8.54 NA
## # ℹ 6 more variables: `Month 05` <dbl>, `Month 06` <dbl>, `Month 07` <dbl>,
## # `Month 08` <dbl>, `Month 10` <dbl>, `Month 11` <dbl>
We want to find the best location for an amusement park that isn’t too hot, or too cold. Define an appropriate temperature range where it is comfortable to be outside. Then, create a graph showing how different locations meet your temperature requirement.
Write a brief 2-3 sentence explanation of your findings.
# Assuming you have the ggplot2 library installed
library(ggplot2)
# Create a ggplot object
ggplot(summary_table_90, aes(x = month, y = temp_90p, color = STATION)) +
geom_line() +
geom_point() +
labs(title = "Highest Temp Over Time by Station",
x = "Month",
y = "Highest Temp (°C)",
color = "Station") +
theme(legend.position = "top")
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?
# Create a ggplot object
ggplot(summary_table_10, aes(x = month, y = temp_10p, color = STATION)) +
geom_line() +
geom_point() +
labs(title = "Lowest Temp Over Time by Station",
x = "Month",
y = "Lowest Temp (°C)",
color = "Station") +
theme(legend.position = "top")
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?
# Create a ggplot object
ggplot(summary_table, aes(x = month, y = avg_temp, color = STATION)) +
geom_line() +
geom_point() +
labs(title = "Average Temperature Over Time by Station",
x = "Month",
y = "Average Temperature (°C)",
color = "Station") +
theme(legend.position = "top")
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?
****As seen in the graphs above, we are looking for a park that is not too hot ot too cold. I would pick either station 04725 or 14762
Which of the sites would be best as an airport? Explore the others variables in your dataset. For example, look at cloud cover or average wind speed. This may require some data clean, so carefully look at your dataset for weird values.
Give a chart, and write a 2-3 sentence explanation of your answer.
***This makes it seem that wind speed is not a factor
# Create a ggplot object
ggplot(summary_table_w, aes(x = month, y =avg_wind , color = STATION)) +
geom_line() +
geom_point() +
labs(title = "Wind Over Time by Station",
x = "Month",
y = "Wind Speed",
color = "Station") +
theme(legend.position = "top")
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?