Skip to content

Welcome to New York City, one of the most-visited cities in the world. There are many Airbnb listings in New York City to meet the high demand for temporary lodging for travelers, which can be anywhere between a few nights to many months. In this notebook, we will take a closer look at the New York Airbnb market by combining data from multiple file types like .csv, .tsv, and .xlsx (Excel files).

Recall that CSV, TSV, and Excel files are three common formats for storing data. Three files containing data on 2019 Airbnb listings are available to you:

datasets/airbnb_price.csv

  • listing_id: unique identifier of listing
  • price: nightly listing price in USD
  • nbhood_full: name of borough and neighborhood where listing is located

datasets/airbnb_room_type.xlsx This is an Excel file containing data on Airbnb listing descriptions and room types.

  • listing_id: unique identifier of listing
  • description: listing description
  • room_type: Airbnb has three types of rooms: shared rooms, private rooms, and entire homes/apartments

datasets/airbnb_last_review.tsv This is a TSV file containing data on Airbnb host names and review dates.

  • listing_id: unique identifier of listing
  • host_name: name of listing host
  • last_review: date when the listing was last reviewed
# We've loaded your first few packages for you in the first cell. Please feel free to add as many cells as you like!
suppressMessages(library(dplyr)) # This line is required to check your answer correctly
options(readr.show_types = FALSE) # This line is required to check your answer correctly
library(readr)
library(readxl)
library(stringr)
library(lubridate)

Read the datasets first, then combine them into one dataframe.

# read datasets
airbnb_price <- read_csv("data/airbnb_price.csv")
airbnb_last_review <- read_tsv("data/airbnb_last_review.tsv") 
airbnb_room_type <- read_excel("data/airbnb_room_type.xlsx")

# join datasets
listings <- airbnb_price %>%
	inner_join(airbnb_last_review, by = "listing_id") %>%
	inner_join(airbnb_room_type, by = "listing_id")

#glimpse the dataframe
glimpse(listings)

Question 1: When was the earliest and most recent reviews in the dataset?

review_dates <- listings %>% 
#covert last_review to date, so that min() and max() could be used.
	mutate(last_review = mdy(last_review)) %>%
	summarize(first_reviewed = min(last_review),
              last_reviewed = max(last_review))
review_dates

Question 2: How many listings are private rooms?

review_dates$nb_private_rooms <- listings %>% 
# the room type have some inconsistant writings or capital letters. The easier way is to change them all to small letter. 
	mutate(room_type = tolower(room_type) ) %>%
# count room_type
	count(room_type) %>%
# filter only for private room
	filter(room_type == "private room") %>%
# get the numer of private room as numeric vector.
	pull(n)
review_dates

Question 3: What is the average listing price in the dataset?

review_dates$avg_price <- listings %>%
# the " dollars" in the price column needs to be removed. And the price should be numeric.
	mutate(price = str_remove(price, " dollars") %>% as.numeric()) %>%
	summarize(avg_price = mean(price)) %>%
# convert tibble to vector.
	as.numeric()

review_dates