Challenge 2

Data wrangling: using group() and summarise()
Author

Calvin Cuff

Published

December 27, 2023

library(dplyr)
library(here)
library(readxl)
library(readr)
knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE)

Challenge Overview

Today’s challenge is to

  1. read in a data set, and describe the data using both words and any supporting information (e.g., tables, etc)
  2. provide summary statistics for different interesting groups within the data, and interpret those statistics

Read in the Data

Read in one (or more) of the following data sets, using the correct R package and command.

  • railroad*.csv or StateCounty2012.xls ⭐
  • FAOstat*.csv or birds.csv ⭐⭐⭐
  • hotel_bookings.csv ⭐⭐⭐⭐

Add any comments or documentation as needed. More challenging data may require additional code chunks and documentation.

hotels <- read_csv(here("data", "hotel_bookings.csv"))
head(hotels)
# A tibble: 6 × 32
  hotel        is_canceled lead_time arrival_date_year arrival_date_month
  <chr>              <dbl>     <dbl>             <dbl> <chr>             
1 Resort Hotel           0       342              2015 July              
2 Resort Hotel           0       737              2015 July              
3 Resort Hotel           0         7              2015 July              
4 Resort Hotel           0        13              2015 July              
5 Resort Hotel           0        14              2015 July              
6 Resort Hotel           0        14              2015 July              
# ℹ 27 more variables: arrival_date_week_number <dbl>,
#   arrival_date_day_of_month <dbl>, stays_in_weekend_nights <dbl>,
#   stays_in_week_nights <dbl>, adults <dbl>, children <dbl>, babies <dbl>,
#   meal <chr>, country <chr>, market_segment <chr>,
#   distribution_channel <chr>, is_repeated_guest <dbl>,
#   previous_cancellations <dbl>, previous_bookings_not_canceled <dbl>,
#   reserved_room_type <chr>, assigned_room_type <chr>, …

Describe the data

Using a combination of words and results of R commands, can you provide a brief, high level description of the data? Describe as efficiently as possible where/how the data was (likely) gathered, indicate the cases and variables (both the interpretation and any details you deem useful to the reader to fully understand your chosen data).

dim(hotels)
[1] 119390     32
colnames(hotels)
 [1] "hotel"                          "is_canceled"                   
 [3] "lead_time"                      "arrival_date_year"             
 [5] "arrival_date_month"             "arrival_date_week_number"      
 [7] "arrival_date_day_of_month"      "stays_in_weekend_nights"       
 [9] "stays_in_week_nights"           "adults"                        
[11] "children"                       "babies"                        
[13] "meal"                           "country"                       
[15] "market_segment"                 "distribution_channel"          
[17] "is_repeated_guest"              "previous_cancellations"        
[19] "previous_bookings_not_canceled" "reserved_room_type"            
[21] "assigned_room_type"             "booking_changes"               
[23] "deposit_type"                   "agent"                         
[25] "company"                        "days_in_waiting_list"          
[27] "customer_type"                  "adr"                           
[29] "required_car_parking_spaces"    "total_of_special_requests"     
[31] "reservation_status"             "reservation_status_date"       
unique(hotels$arrival_date_year)
[1] 2015 2016 2017
unique(hotels$hotel)
[1] "Resort Hotel" "City Hotel"  
unique(hotels$country)
  [1] "PRT"  "GBR"  "USA"  "ESP"  "IRL"  "FRA"  "NULL" "ROU"  "NOR"  "OMN" 
 [11] "ARG"  "POL"  "DEU"  "BEL"  "CHE"  "CN"   "GRC"  "ITA"  "NLD"  "DNK" 
 [21] "RUS"  "SWE"  "AUS"  "EST"  "CZE"  "BRA"  "FIN"  "MOZ"  "BWA"  "LUX" 
 [31] "SVN"  "ALB"  "IND"  "CHN"  "MEX"  "MAR"  "UKR"  "SMR"  "LVA"  "PRI" 
 [41] "SRB"  "CHL"  "AUT"  "BLR"  "LTU"  "TUR"  "ZAF"  "AGO"  "ISR"  "CYM" 
 [51] "ZMB"  "CPV"  "ZWE"  "DZA"  "KOR"  "CRI"  "HUN"  "ARE"  "TUN"  "JAM" 
 [61] "HRV"  "HKG"  "IRN"  "GEO"  "AND"  "GIB"  "URY"  "JEY"  "CAF"  "CYP" 
 [71] "COL"  "GGY"  "KWT"  "NGA"  "MDV"  "VEN"  "SVK"  "FJI"  "KAZ"  "PAK" 
 [81] "IDN"  "LBN"  "PHL"  "SEN"  "SYC"  "AZE"  "BHR"  "NZL"  "THA"  "DOM" 
 [91] "MKD"  "MYS"  "ARM"  "JPN"  "LKA"  "CUB"  "CMR"  "BIH"  "MUS"  "COM" 
[101] "SUR"  "UGA"  "BGR"  "CIV"  "JOR"  "SYR"  "SGP"  "BDI"  "SAU"  "VNM" 
[111] "PLW"  "QAT"  "EGY"  "PER"  "MLT"  "MWI"  "ECU"  "MDG"  "ISL"  "UZB" 
[121] "NPL"  "BHS"  "MAC"  "TGO"  "TWN"  "DJI"  "STP"  "KNA"  "ETH"  "IRQ" 
[131] "HND"  "RWA"  "KHM"  "MCO"  "BGD"  "IMN"  "TJK"  "NIC"  "BEN"  "VGB" 
[141] "TZA"  "GAB"  "GHA"  "TMP"  "GLP"  "KEN"  "LIE"  "GNB"  "MNE"  "UMI" 
[151] "MYT"  "FRO"  "MMR"  "PAN"  "BFA"  "LBY"  "MLI"  "NAM"  "BOL"  "PRY" 
[161] "BRB"  "ABW"  "AIA"  "SLV"  "DMA"  "PYF"  "GUY"  "LCA"  "ATA"  "GTM" 
[171] "ASM"  "MRT"  "NCL"  "KIR"  "SDN"  "ATF"  "SLE"  "LAO" 

This data set captures 119390 hotel bookings from different countries in the years 2015, 2016 and 2017. There are 119390 rows and 32 columns describing detailed characteristics of the bookings such as the length of stay, details of the reservation, and the guest’s profile. There are only 2 unique hotels identified, as Resort Hotel and City Hotel, which accepted bookings from customers across 178 different countries.

Provide Grouped Summary Statistics

Conduct some exploratory data analysis, using dplyr commands such as group_by(), select(), filter(), and summarise(). Find the central tendency (mean, median, mode) and dispersion (standard deviation, mix/max/quantile) for different subgroups within the data set. Describe what you find.

hotels %>%
  # Group by year
  group_by(arrival_date_year) %>%
  # Select relevant columns
  select(hotel, country, arrival_date_year, adr) %>%
  # Summarize the grouped data
  summarize(mean_price=mean(adr), median_price=median(adr), price_standard_deviation=sd(adr))
# A tibble: 3 × 4
  arrival_date_year mean_price median_price price_standard_deviation
              <dbl>      <dbl>        <dbl>                    <dbl>
1              2015       87.2         77.8                     42.7
2              2016       98.3         91.0                     50.0
3              2017      115.         107.                      52.3

We can see the hotel data grouped by year indicates that there is an increasing trend in the average daily rate of a hotel room in more recent years.

hotels %>%
  # Grouping by hotel
  group_by(hotel) %>%
  # Select relevant columns
  select(hotel, country, arrival_date_year, adr, lead_time, is_canceled) %>%
  # Summarize the grouped data
  summarize(mean_price=mean(adr), median_price=median(adr), mean_lead_time=mean(lead_time), pct_cancellations=sum(is_canceled)/n())
# A tibble: 2 × 5
  hotel        mean_price median_price mean_lead_time pct_cancellations
  <chr>             <dbl>        <dbl>          <dbl>             <dbl>
1 City Hotel        105.          99.9          110.              0.417
2 Resort Hotel       95.0         75             92.7             0.278

We can see the hotel data grouped by hotel, city vs resort, indicates that the City Hotel is more expensive than the Resort Hotel, even though the reservations are typically booked with 2 and a half weeks more lead time. It is also interesting that the City Hotel has more than a 41 percent cancellation rate for its bookings, compared to the 27 percent of the Resort Hotel.

hotels %>%
  # Mutate to add new column 'season' which is derived from 'arrival_month_date'
  mutate(season = case_when(
    arrival_date_month == "January" | arrival_date_month == "February" | arrival_date_month == "December" ~ "Winter",
    arrival_date_month == "March" | arrival_date_month == "April" | arrival_date_month == "May" ~ "Spring",
    arrival_date_month == "June" | arrival_date_month == "July" | arrival_date_month == "August" ~ "Summer",
    arrival_date_month == "September" | arrival_date_month == "October" | arrival_date_month == "November" ~ "Fall",
  )) %>%
  # Select relevant columns
  select(hotel, country, arrival_date_year, adr, lead_time, season, stays_in_weekend_nights, stays_in_week_nights,is_canceled) %>%
  # Groupings by hotel and season
  group_by(hotel, season) %>%
  # Summarize data
  summarize(mean_price=mean(adr), median_price=median(adr), mean_length_of_stay=mean(stays_in_weekend_nights+stays_in_week_nights), num_bookings=n()) %>%
  # Arrange by the number of bookings
  arrange(num_bookings)
# A tibble: 8 × 6
# Groups:   hotel [2]
  hotel        season mean_price median_price mean_length_of_stay num_bookings
  <chr>        <chr>       <dbl>        <dbl>               <dbl>        <int>
1 Resort Hotel Winter       58.2         50.8                3.10         7944
2 Resort Hotel Fall         69.0         61.4                4.23         9100
3 Resort Hotel Spring       71.7         68                  4.15        10504
4 Resort Hotel Summer      157.         153                  5.30        12512
5 City Hotel   Winter       85.6         80                  3.06        12833
6 City Hotel   Fall        101.          96.4                2.82        19362
7 City Hotel   Spring      110.         103                  2.97        22170
8 City Hotel   Summer      115.         112.                 3.07        24965

If we group the data by hotel and a new column season, which is a derivative of the arrival month, there are some interesting insights about pricing and popularity. First we can see that the number of bookings per hotel increases during the same seasons, Winter is the least busiest, then Fall, Spring, and Summer being the most popular. Even in the City Hotel’s slowest season Winter, it has more bookings than the busiest season of the Resort Hotel, Summer. The City Hotel is also more expensive than the Resort Hotel in every season except for Summer, when the price of the Resort Hotel drastically increases to the most expensive price per night of either hotel. The grouped data also shows that on average guests will always stay longer at the Resort Hotel vs the City Hotel, which makes sense if people were traveling for leisure to the Resort Hotel and maybe attending to shorter business needs for the City Hotel.