Challenge 2 Paritosh

challenge_2
railroads
faostat
hotel_bookings
Data wrangling: using group() and summarise()
Author

Paritosh Gandhi

Published

May 26, 2023

Code
library(tidyverse)

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, available in the posts/_data folder, using the correct R package and command.

  • railroad*.csv or StateCounty2012.xls ⭐
  • FAOstat*.csv or birds.csv ⭐⭐⭐
  • hotel_bookings.csv ⭐⭐⭐⭐
Code
htb <- read.csv(file = '_data/hotel_bookings.csv')




#unique(df$adults)

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

Describe the data

Using a combination of words and results of R commands, can you provide a 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).

Code
unique(htb$hotel)
[1] "Resort Hotel" "City Hotel"  
Code
unique(htb$arrival_date_year)
[1] 2015 2016 2017
Code
unique(htb$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" 
Code
unique(htb$market_segment)
[1] "Direct"        "Corporate"     "Online TA"     "Offline TA/TO"
[5] "Complementary" "Groups"        "Undefined"     "Aviation"     
Code
unique(htb$reserved_room_type)
 [1] "C" "A" "D" "E" "G" "F" "H" "L" "P" "B"
Code
unique(htb$arrival_date_month)
 [1] "July"      "August"    "September" "October"   "November"  "December" 
 [7] "January"   "February"  "March"     "April"     "May"       "June"     

Hence, the dataset contains booking data for two types of hotels, in 3 years time frame. The guests are from 177 countries and 10 types of rooms.


## 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.

Code
htb %>% 
  filter(hotel == "City Hotel") %>% 
  group_by(hotel) %>% 
  summarize(avg_adr = mean(adr))
# A tibble: 1 × 2
  hotel      avg_adr
  <chr>        <dbl>
1 City Hotel    105.
Code
htb %>% 
  summarize(avg_adr = mean(adr))
   avg_adr
1 101.8311
Code
htb %>% 
  filter(hotel == "Resort Hotel", country %in% c("PRT","GBR","USA","","FRA","POL")) %>% 
  group_by(arrival_date_month) %>% 
  summarise(avg_adr = mean(adr))
# A tibble: 12 × 2
   arrival_date_month avg_adr
   <chr>                <dbl>
 1 April                 74.7
 2 August               181. 
 3 December              72.5
 4 February              54.5
 5 January               48.2
 6 July                 148. 
 7 June                 105. 
 8 March                 56.1
 9 May                   74.6
10 November              46.5
11 October               58.3
12 September             91.3

Explain and Interpret

Be sure to explain why you choose a specific group. Comment on the interpretation of any interesting differences between groups that you uncover. This section can be integrated with the exploratory data analysis, just be sure it is included.

Code
table_1 <- htb%>%
  group_by(hotel, reserved_room_type)%>%
  summarise(price = mean(adr),
            adults = mean(adults),
            children = mean(children+babies, na.rm=TRUE)
            )%>%
  pivot_wider(names_from= hotel, 
              values_from = c(price, adults, children))
table_1
# A tibble: 10 × 7
   reserved_room_type `price_City Hotel` price…¹ adult…² adult…³ child…⁴ child…⁵
   <chr>                           <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
 1 A                                96.2    76.2    1.78    1.78  0.0414  0.0370
 2 B                                90.3   105.     1.57    2     0.567   0     
 3 C                                85.5   161.     1.5     2.05  0.143   1.36  
 4 D                               131.    104.     2.17    1.96  0.0456  0.0572
 5 E                               157.    114.     2.07    1.95  0.281   0.0498
 6 F                               189.    133.     2.01    1.97  1.64    0.0904
 7 G                               202.    168.     2.27    2.02  1.05    1.38  
 8 P                                 0       0      0       0     0       0     
 9 H                                NA     188.    NA       2.71 NA       0.995 
10 L                                NA     125.    NA       2.17 NA       0     
# … with abbreviated variable names ¹​`price_Resort Hotel`,
#   ²​`adults_City Hotel`, ³​`adults_Resort Hotel`, ⁴​`children_City Hotel`,
#   ⁵​`children_Resort Hotel`