Setup

Download Data
Airbnb listings from Washington DC

# Load the tidyverse package.
library(tidyverse) 

# Read in the data and store it as a data frame object called "airbnb".
airbnb <- read_csv("data/listings.csv")

1. What are the different types of cancellation policies offered at Airbnb?

Answer
flexible, moderate, strict and super strict

cancellation_policy is the name of a column in the Airbnb data set, and it is a categorical variable. The table function allows us to view all of the categories within a categorical variable.

# The dollar sign is used to reference the column name within the airbnb dataset.
table(airbnb$cancellation_policy)
## 
##        flexible        moderate          strict super_strict_30 
##            2816            2153            2743              76

2. What is the most common and least common cancellation policy?

Answer
most common: flexible
least common: super strict

Nest the table function inside of a sort function to sort our categories by size. If we set the decreasing argument to TRUE, it will sort the categories from largest to smallest.

sort(table(airbnb$cancellation_policy), decreasing = TRUE)
## 
##        flexible          strict        moderate super_strict_30 
##            2816            2743            2153              76

3. What percent of Airbnb property types are houses? Hint: To find what percent of X is Y, use this formula: Y/X * 100

Answer
31.84%

Use the table function to find out how many property types are houses.

table(airbnb$property_type)
## 
##          Apartment    Bed & Breakfast               Boat     Boutique hotel 
##               4191                 73                  3                 11 
##           Bungalow              Cabin             Castle        Condominium 
##                  4                  1                  1                420 
##               Dorm        Guest suite         Guesthouse             Hostel 
##                 25                 11                 17                  2 
##              House             In-law               Loft              Other 
##               2480                 12                 33                 31 
## Serviced apartment          Timeshare          Townhouse              Train 
##                  1                  1                468                  1 
##          Treehouse              Villa 
##                  1                  1

There are 2,480 houses. Now we need to know the total number of properties overall. Each property has its own row in the airbnb data set, so we can use the dim function to see how many rows are in the data set.

dim(airbnb)
## [1] 7788   14

The first number it give us is the number of rows and the second number it give us is the number of columns. Now we know that we have 2,480 houses out of 7,788 properties total, so we just need to use our formula to find the percentage.

(2480 / 7788) * 100
## [1] 31.84386

4. How many listings get a review rating below 50?

Answer
16

review_scores_rating is a column within the airbnb data set. So, if we run airbnb$review_scores_rating < 50, R will give us a long list that reads “TRUE” or “FALSE” for each property depending on whether it received a rating less than 50. However, we don’t want to manually count each “TRUE” or “FALSE”. Instead, we need to use the table function to see the total numbers for each.

table(airbnb$review_scores_rating < 50)
## 
## FALSE  TRUE 
##  5565    16

5. Try generating a scatterplot of price against bedrooms. Now add an overlay of a loess line. Now try creating subplots for each of the property types.

Answer

Create a scatterplot with price on the x axis and bedrooms on the y axis.

ggplot(airbnb, aes(x = price, y = bedrooms)) +
  geom_point()

Use geom_smooth() to add a loess Line over the scatterplot.

ggplot(airbnb, aes(x = price, y = bedrooms)) +
  geom_point() +
  geom_smooth()

Use facet_wrap to generate subplots for each property type.

ggplot(airbnb, aes(x = price, y = bedrooms)) +
  geom_point() +
  geom_smooth() + 
  facet_wrap(~ property_type)

6. Try installing the visdat library, and explore different ways to visualize missing data.

Answer

Install the visdat library. You will only have to do this once.

install.packages("visdat")

Load the visdat library. You will have to do this each time you open a new session in R and want to use the package.

library(visdat)

Use ? to read the documentation on the package you just installed. There will be a list of links for each of the main functions. Click on each link and read about each function.

?visdat

Use vis_miss to visualize missing data in the airbnb data set. sort_miss will sort the columns in order of most to least missingness.

vis_miss(airbnb, sort_miss = TRUE)