library(tidyverse)
library(ggplot2)
::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE) knitr
Challenge 9
challenge_9
Creating a function
Reading in Data
The data set that was chosen was the hotel bookings csv file. This was chosen because many of the variables have numerical observations, and summary statistics (mean, median, max, etc) can be calculated.
= read_csv("_data/hotel_bookings.csv")
hotel hotel
Creating and Describing Function
I created a function that takes in one input, a chosen column from a dataset, and finds basic summary statistics for the chosen column. The statistics were also formatted to be 3 decimal points in the function. The function then returns the results in their respective labelled columns. Three columns were chosen as examples of how the function works.
<- function(column){
func <- round(mean(column, na.rm = TRUE), 3)
meanHotel <- round(median(column, na.rm = TRUE), 3)
medianHotel <- round(max(column, na.rm = TRUE), 3)
maxHotel <- round(min(column, na.rm = TRUE),3) #finds basic summary stats and formats them to 3 decimal points
minHotel return(c(Mean = meanHotel, Median = medianHotel, Max = maxHotel, Min = minHotel)) #returns the values in their respective columns
}
= func(hotel$adults)
adultsAnswer adultsAnswer
Mean Median Max Min
1.856 2.000 55.000 0.000
= func(hotel$children)
childrenAnswer childrenAnswer
Mean Median Max Min
0.104 0.000 10.000 0.000
= func(hotel$babies)
babiesAnswer babiesAnswer
Mean Median Max Min
8e-03 0e+00 1e+01 0e+00