Week 5 - Assignment

Author

G Oliveira

Create a Data Visualization

#install.packages("readxl")
library(readxl)
library(ggplot2)
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
df <- read_excel("/Users/giovanarodrigues/Desktop/DATA 110/dataset/Airbnb_DC_25.csv")
head(df)
# A tibble: 6 × 18
     id name        host_id host_name neighbourhood_group neighbourhood latitude
  <dbl> <chr>         <dbl> <chr>     <lgl>               <chr>            <dbl>
1  3686 Vita's Hid…    4645 Vita      NA                  Historic Ana…     38.9
2  3943 Historic R…    5059 Vasa      NA                  Edgewood, Bl…     38.9
3  4197 Capitol Hi…    5061 Sandra    NA                  Capitol Hill…     38.9
4  4529 Bertina's …    5803 Bertina   NA                  Eastland Gar…     38.9
5  5589 Cozy apt i…    6527 Ami       NA                  Kalorama Hei…     38.9
6  7103 Lovely gue…   17633 Charlotte NA                  Spring Valle…     38.9
# ℹ 11 more variables: longitude <dbl>, room_type <chr>, price <dbl>,
#   minimum_nights <dbl>, number_of_reviews <dbl>, last_review <dttm>,
#   reviews_per_month <dbl>, calculated_host_listings_count <dbl>,
#   availability_365 <dbl>, number_of_reviews_ltm <dbl>, license <chr>

Inspecting the Data

colSums(is.na(df))
                            id                           name 
                             0                              0 
                       host_id                      host_name 
                             0                              2 
           neighbourhood_group                  neighbourhood 
                          6257                              0 
                      latitude                      longitude 
                             0                              0 
                     room_type                          price 
                             0                           1488 
                minimum_nights              number_of_reviews 
                             0                              0 
                   last_review              reviews_per_month 
                          1236                           1236 
calculated_host_listings_count               availability_365 
                             0                              0 
         number_of_reviews_ltm                        license 
                             0                           1560 
summary(df)
       id                   name         host_id              host_name   
 Min.   :3.686e+03   Length   :6257   Min.   :     4617   Length   :6257  
 1st Qu.:3.792e+07   N.unique :5996   1st Qu.: 22024017   N.unique :1860  
 Median :7.501e+17   N.blank  :   0   Median : 81005284   N.blank  :   0  
 Mean   :6.159e+17   Min.nchar:   4   Mean   :176451046   Min.nchar:   1  
 3rd Qu.:1.143e+18   Max.nchar: 107   3rd Qu.:304261532   Max.nchar:  34  
 Max.   :1.375e+18                    Max.   :681391481   NAs      :   2  
                                                                          
 neighbourhood_group   neighbourhood     latitude       longitude     
 Mode:logical        Length   :6257   Min.   :38.82   Min.   :-77.11  
 NAs :6257           N.unique :  39   1st Qu.:38.90   1st Qu.:-77.03  
                     N.blank  :   0   Median :38.91   Median :-77.01  
                     Min.nchar:  18   Mean   :38.91   Mean   :-77.01  
                     Max.nchar:  97   3rd Qu.:38.92   3rd Qu.:-76.99  
                                      Max.   :38.99   Max.   :-76.91  
                                                                      
     room_type        price        minimum_nights   number_of_reviews
 Length   :6257   Min.   :  10.0   Min.   :  1.00   Min.   :   0.00  
 N.unique :   4   1st Qu.:  88.0   1st Qu.:  1.00   1st Qu.:   1.00  
 N.blank  :   0   Median : 131.0   Median :  2.00   Median :  19.00  
 Min.nchar:  10   Mean   : 168.7   Mean   : 13.23   Mean   :  66.38  
 Max.nchar:  15   3rd Qu.: 193.0   3rd Qu.: 31.00   3rd Qu.:  86.00  
                  Max.   :7000.0   Max.   :701.00   Max.   :1205.00  
                  NAs    :1488                                       
  last_review                  reviews_per_month calculated_host_listings_count
 Min.   :2013-06-15 00:00:00   Min.   : 0.010    Min.   :  1.00                
 1st Qu.:2024-10-17 00:00:00   1st Qu.: 0.470    1st Qu.:  1.00                
 Median :2025-01-23 00:00:00   Median : 1.460    Median :  3.00                
 Mean   :2024-09-12 12:48:19   Mean   : 1.974    Mean   : 33.15                
 3rd Qu.:2025-02-27 00:00:00   3rd Qu.: 2.940    3rd Qu.: 14.00                
 Max.   :2025-03-14 00:00:00   Max.   :28.200    Max.   :289.00                
 NAs    :1236                  NAs    :1236                                    
 availability_365 number_of_reviews_ltm      license    
 Min.   :  0.0    Min.   :  0.0         Length   :6257  
 1st Qu.: 43.0    1st Qu.:  0.0         N.unique :2459  
 Median :175.0    Median :  5.0         N.blank  :   0  
 Mean   :175.8    Mean   : 15.8         Min.nchar:   6  
 3rd Qu.:303.0    3rd Qu.: 25.0         Max.nchar:  72  
 Max.   :365.0    Max.   :290.0         NAs      :1560  
                                                        

Cleaning the Data

df_nona <- df |> filter(!is.na(price)) #removing NA from price
df_plot <- df_nona |>
  filter(price > 50, price <400) |>
  group_by(room_type) |>
  summarise(count = n(),
            median_price = median(price))
head(df_plot)
# A tibble: 4 × 3
  room_type       count median_price
  <chr>           <int>        <dbl>
1 Entire home/apt  3350          142
2 Hotel room         43          248
3 Private room      825           84
4 Shared room         9           72

Creating the Graph

ggplot(df_plot, aes(room_type, median_price, fill = room_type)) +
  geom_col() +
  scale_fill_manual (values = c("darkblue", "darkred", "orange", "pink")) +
  labs(
    title = "Median Airbnb Price by Room Type in Washington, DC",
    x = "Room Type",
    y = "Median Nightly Price (USD)",
    fill = "Room Type",
    caption = "Source: Airbnb_DC_25.csv")

This bar graph shows the median nightly price of Airbnb in Washington, DC, by room type. Each room type is in a different color. I used ‘dplyr’ to remove NA (missing values) from the column ‘price’ and filtered values to exclude outlier in order to be as close to the mean as possible and calculated the median for each group. The plot shows that Hotel rooms have much higher median prices than shared rooms. That suggests that staying at a hotel coast roughly 2 times more than sharing a room.