library(learnr) # remotes::install_github("rstudio/learnr")
options(dplyr.summarise.inform = FALSE)
library(tidyverse)
library(skimr)
knitr::opts_chunk$set(echo = FALSE)

if (!file.exists("hotels.rds")) {
  hotels <- paste0(
    "https://raw.githubusercontent.com/",
    "rfordatascience/tidytuesday/",
    "master/data/2020/2020-02-11/hotels.csv"
    ) %>% 
    read_csv()
  saveRDS(hotels, "hotels.rds")
} else {
  hotels <- readRDS("hotels.rds")
}

Introduction

library(tidyverse)
library(skimr)

This is a dataset of real booking data from two hotels in Portugal, shared by Antonio, Almeida and Nunes, 2019 and preprocessed as described in this #TidyTuesday dataset.

One is a city hotel and one is a resort hotel.

Warm up! Take a look at an overview of the data with the skim() function.

A definition of all variables is given in the [Data dictionary] section at the end, though you don't need to familiarize yourself with all variables in order to work through these exercises.

skim(hotels)

Exercise 1.

Are people traveling on a whim? Let's see...

Fill in the blanks for filtering for hotel bookings where the guest is from Portugal (country code "PRT") and the lead_time is less than 1 day.

hotels %>%
  filter(
    country ____ "PRT", 
    lead_time ____ ____
    )
hotels %>%
  filter(
    country != "PRT", 
    lead_time < 1
    )

Exercise 2

How many bookings involve at least 1 child or baby?

In the following chunk, replace

hotels %>%
  filter(
    children [AT LEAST] 1 [OR] babies [AT LEAST] 1
    )
hotels %>%
  filter(
    children >= 1 | babies >=  1
    )

Exercise 3

Do you think it's more likely to find bookings with children or babies in city hotels or resort hotels? Test your intuition. Using filter() and nrow(), determine the number of bookings in the resort hotel (coded as Resort Hotel) that have a child or baby in the room.

hotels %>%
  filter(___) %>% 
  nrow()
hotels %>%
  filter(children >= 1 | babies >=  1,
         hotel == "Resort Hotel") %>%
  nrow()

Then, do the same for the city hotel (coded as City Hotel), and compare the numbers of rows in the resulting filtered tibbles.


Exercise 4

Create a frequency table of the number of adults in a booking. (Use group_by.) Display the results in descending order so the most common observation is on top. What is the most common number of adults in bookings in this dataset? Are there any surprising results?


hotels %>% 
  group_by(adults)  %>% 
  summarize(n=n()) %>% 
  arrange(desc(n))

Exercise 5

Repeat Exercise 5, once for canceled bookings (is_canceled coded as 1) and once for not canceled bookings (is_canceled coded as 0). What does this reveal about the surprising results you spotted in the previous exercise?


hotels %>% 
  filter(is_canceled == 1) %>% 
  count(adults) %>% 
  arrange(desc(n))
hotels %>% 
  filter(is_canceled == 0) %>% 
  count(adults) %>% 
  arrange(desc(n))

Exercise 6

Calculate minimum, mean, median, and maximum average daily rate (adr) grouped by hotel type so that you can get these statistics separately for the resort and city hotel. Which hotel is more expensive?


hotels %>%
  group_by(hotel) %>%
  summarize(
    min_adr = min(adr),
    mean_adr = mean(adr),
    median_adr = median(adr),
    max_adr = max(adr)
  )
  # or, using a fancy new function called `across`:
  # summarize(across(adr, list(min = min, mean = mean, median = median, max = max)))

Exercise 7

We observe two unusual values in the summary statistics above -- a negative minimum, and a very high maximum). What types of hotels are these? Locate these observations in the dataset and find out the arrival date (year and month) as well as how many people (adults, children, and babies) stayed in the room. You can investigate the data in the viewer to locate these values, but preferably you should identify them in a reproducible way with some code.

Hint: For example, you can filter for the given adr amounts and select the relevant columns.


hotels %>% 
  filter(adr > 5000) %>%
  select(hotel, arrival_date_year, arrival_date_month, adults, children, babies, is_canceled)
hotels %>% 
  filter(adr < 0) %>%
  select(hotel, arrival_date_year, arrival_date_month, adults, children, babies, is_canceled, adr)

Data dictionary

Below is the full data dictionary. Note that it is long (there are lots of variables in the data), but we will be using a limited set of the variables for our analysis.

|variable |class |description | |:------------------------------|:---------|:-----------| |hotel |character | Hotel (H1 = Resort Hotel or H2 = City Hotel) | |is_canceled |double | Value indicating if the booking was canceled (1) or not (0) | |lead_time |double | Number of days that elapsed between the entering date of the booking into the PMS and the arrival date | |arrival_date_year |double | Year of arrival date| |arrival_date_month |character | Month of arrival date| |arrival_date_week_number |double | Week number of year for arrival date| |arrival_date_day_of_month |double | Day of arrival date| |stays_in_weekend_nights |double | Number of weekend nights (Saturday or Sunday) the guest stayed or booked to stay at the hotel | |stays_in_week_nights |double | Number of week nights (Monday to Friday) the guest stayed or booked to stay at the hotel| |adults |double | Number of adults| |children |double | Number of children| |babies |double |Number of babies | |meal |character | Type of meal booked. Categories are presented in standard hospitality meal packages:
Undefined/SC – no meal package;
BB – Bed & Breakfast;
HB – Half board (breakfast and one other meal – usually dinner);
FB – Full board (breakfast, lunch and dinner) | |country |character | Country of origin. Categories are represented in the ISO 3155–3:2013 format | |market_segment |character | Market segment designation. In categories, the term “TA” means “Travel Agents” and “TO” means “Tour Operators” | |distribution_channel |character | Booking distribution channel. The term “TA” means “Travel Agents” and “TO” means “Tour Operators” | |is_repeated_guest |double | Value indicating if the booking name was from a repeated guest (1) or not (0) | |previous_cancellations |double | Number of previous bookings that were cancelled by the customer prior to the current booking | |previous_bookings_not_canceled |double | Number of previous bookings not cancelled by the customer prior to the current booking | |reserved_room_type |character | Code of room type reserved. Code is presented instead of designation for anonymity reasons | |assigned_room_type |character | Code for the type of room assigned to the booking. Sometimes the assigned room type differs from the reserved room type due to hotel operation reasons (e.g. overbooking) or by customer request. Code is presented instead of designation for anonymity reasons | |booking_changes |double | Number of changes/amendments made to the booking from the moment the booking was entered on the PMS until the moment of check-in or cancellation| |deposit_type |character | Indication on if the customer made a deposit to guarantee the booking. This variable can assume three categories:
No Deposit – no deposit was made;
Non Refund – a deposit was made in the value of the total stay cost;
Refundable – a deposit was made with a value under the total cost of stay. | |agent |character | ID of the travel agency that made the booking | |company |character | ID of the company/entity that made the booking or responsible for paying the booking. ID is presented instead of designation for anonymity reasons | |days_in_waiting_list |double | Number of days the booking was in the waiting list before it was confirmed to the customer | |customer_type |character | Type of booking, assuming one of four categories:
Contract - when the booking has an allotment or other type of contract associated to it;
Group – when the booking is associated to a group;
Transient – when the booking is not part of a group or contract, and is not associated to other transient booking;
Transient-party – when the booking is transient, but is associated to at least other transient booking| |adr |double | Average Daily Rate as defined by dividing the sum of all lodging transactions by the total number of staying nights | |required_car_parking_spaces |double | Number of car parking spaces required by the customer | |total_of_special_requests |double | Number of special requests made by the customer (e.g. twin bed or high floor)| |reservation_status |character | Reservation last status, assuming one of three categories:
Canceled – booking was canceled by the customer;
Check-Out – customer has checked in but already departed;
No-Show – customer did not check-in and did inform the hotel of the reason why | |reservation_status_date |double | Date at which the last status was set. This variable can be used in conjunction with the ReservationStatus to understand when was the booking canceled or when did the customer checked-out of the hotel|



CalvinData/ds202calvin documentation built on Nov. 24, 2022, 7:11 p.m.