: The dataset provides monthly average temperatures (in Fahrenheit) for three major U.S. cities: New York, Boston, and Los Angeles, spanning from January to June.
The data below is in a “wide” format. In the context of the tidy data principles outlined by Hadley Wickham, tidy data has the following characteristics:
Given these principles, the data below is not in a tidy format because:
Data: City, Months, and Temperatures.
library(tidyverse)
data <- tibble(
City = c("New York", "Boston", "Los Angeles"),
Jan = c(30, 30, 59),
Feb = c(35, 32, 60),
Mar = c(45, 39, 61),
Apr = c(50, 49, 64),
May = c(65, 59, 66),
Jun = c(74, 68, 71)
)
print(data)
Table 2-1: Wide data tibble
## # A tibble: 3 x 7
## City Jan Feb Mar Apr May Jun
##
## 1 New York 30 35 45 50 65 74
## 2 Boston 30 32 39 49 59 68
## 3 Los Angeles 59 60 61 64 66 71
To transform this data into a tidy format, you’d want to have three columns: “City”, “Month”, and “Temperature”. Each row would then represent the temperature for a specific city in a specific month.
Using the tidyverse
package, transform the data into a tidy format with the pivot_longer
function:
This will give you a tibble where each row represents a single observation: the temperature of a city in a specific month. This format is more versatile for various analyses and visualizations in R.
Table 2-2: Wide to long data
long_data <- data %>%
pivot_longer(cols = Jan:Jun,
names_to = "Month",
values_to = "Temp (F)")
print(long_data)
Table 2-2: Wide to long tibble
## # A tibble: 18 x 3
## City Month `Temp (F)`
##
## 1 New York Jan 30
## 2 New York Feb 35
## 3 New York Mar 45
## 4 New York Apr 50
## 5 New York May 65
## 6 New York Jun 74
## 7 Boston Jan 30
## 8 Boston Feb 32
## 9 Boston Mar 39
## 10 Boston Apr 49
## 11 Boston May 59
## 12 Boston Jun 68
## 13 Los Angeles Jan 59
## 14 Los Angeles Feb 60
## 15 Los Angeles Mar 61
## 16 Los Angeles Apr 64
## 17 Los Angeles May 66
## 18 Los Angeles Jun 71
The average temperature for all three cities across all months.
Data: average temperature
average_temp_per_city <- data %>%
pivot_longer(cols = Jan:Jun,
names_to = "Month",
values_to = "Temp (F)") %>%
group_by(City) %>%
summarise(Average_Temp = mean(`Temp (F)`))
print(average_temp_per_city)
Tabel 2-2: average temperature tibble
## # A tibble: 3 x 2
## City Average_Temp
##
## 1 Boston 46.2
## 2 Los Angeles 63.5
## 3 New York 49.8
Overall Average Temperature:
The combined average temperature across all three cities for the six months is approximately 53.17°F. This value provides a general sense of the temperature trends for these cities during the first half of the year.
City-wise Average Temperature:
New York: New York experiences a steady rise in temperature from January to June. The average temperature for New York over these six months is around 49.83°F. The coldest month is January with an average of 30°F, while June is the warmest at 74°F, indicating a transition from winter to summer.
Boston: Boston’s temperatures are somewhat similar to New York’s, but slightly cooler on average. The six-month average temperature for Boston is approximately 46.17°F. Like New York, Boston’s coldest month is January (30°F), and its warmest is June (68°F).
Los Angeles: Los Angeles has a milder and more consistent temperature range compared to the other two cities. The average temperature for Los Angeles over the six months is about 63.5°F. January, with an average of 59°F, is the coolest month, while June, at 71°F, is the warmest. The relatively small fluctuation in temperature indicates a more temperate climate compared to the other cities.
Comparative Analysis:
Winter Months (Jan-Mar): Los Angeles is considerably warmer than both New York and Boston during the winter months. While New York and Boston experience temperatures around the freezing point in January, Los Angeles enjoys a milder climate with temperatures hovering around 60°F.
Spring to Early Summer (Apr-Jun): As we transition to warmer months, all three cities see a rise in temperature. However, the temperature difference between them narrows. By June, New York and Boston temperatures are closer to those of Los Angeles, though still slightly cooler.
Conclusions:
The first half of the year sees a clear temperature gradient among the three cities, with Los Angeles being the warmest, followed by New York, and then Boston.
While New York and Boston experience significant temperature changes from winter to summer, Los Angeles remains relatively consistent, reflecting the
distinct climatic differences between the East and West coasts of the U.S.
For those seeking milder winter temperatures, Los Angeles is the preferable choice among the three. However, by summer, all three cities offer warm and
pleasant temperatures, suitable for outdoor activities.
This analysis provides a snapshot of the temperature trends for New York, Boston, and Los Angeles during the first half of the year. Further analysis could delve into factors influencing these trends, such as geographical location, altitude, proximity to water bodies, and urban heat island effects.
This analysis provides a snapshot of the temperature trends for New York, Boston, and Los Angeles during the first half of the year. Further analysis could delve into factors influencing these trends, such as geographical location, altitude, proximity to water bodies, and urban heat island effects.