UntitledWeek 6

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
library(ggplot2)
library(stringr)
#install.packages("readxl")
library(readxl)
df<- read_excel("C:\\Users\\MFERN123\\DATA_110\\Airbnb_DC_25.csv")
df
# A tibble: 6,257 × 18
      id name       host_id host_name neighbourhood_group neighbourhood latitude
   <dbl> <chr>        <dbl> <chr>     <lgl>               <chr>            <dbl>
 1  3686 Vita's Hi…    4645 Vita      NA                  Historic Ana…     38.9
 2  3943 Historic …    5059 Vasa      NA                  Edgewood, Bl…     38.9
 3  4197 Capitol H…    5061 Sandra    NA                  Capitol Hill…     38.9
 4  4529 Bertina's…    5803 Bertina   NA                  Eastland Gar…     38.9
 5  5589 Cozy apt …    6527 Ami       NA                  Kalorama Hei…     38.9
 6  7103 Lovely gu…   17633 Charlotte NA                  Spring Valle…     38.9
 7 11785 Sanctuary…   32015 Teresa    NA                  Cathedral He…     38.9
 8 12442 Peaches &…   32015 Teresa    NA                  Cathedral He…     38.9
 9 13744 Heart of …   53927 Victoria  NA                  Columbia Hei…     38.9
10 14218 Quiet Com…   32015 Teresa    NA                  Cathedral He…     38.9
# ℹ 6,247 more rows
# ℹ 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>
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>
names(df)
 [1] "id"                             "name"                          
 [3] "host_id"                        "host_name"                     
 [5] "neighbourhood_group"            "neighbourhood"                 
 [7] "latitude"                       "longitude"                     
 [9] "room_type"                      "price"                         
[11] "minimum_nights"                 "number_of_reviews"             
[13] "last_review"                    "reviews_per_month"             
[15] "calculated_host_listings_count" "availability_365"              
[17] "number_of_reviews_ltm"          "license"                       
dim(df)
[1] 6257   18
glimpse(df)
Rows: 6,257
Columns: 18
$ id                             <dbl> 3686, 3943, 4197, 4529, 5589, 7103, 117…
$ name                           <chr> "Vita's Hideaway", "Historic Rowhouse N…
$ host_id                        <dbl> 4645, 5059, 5061, 5803, 6527, 17633, 32…
$ host_name                      <chr> "Vita", "Vasa", "Sandra", "Bertina", "A…
$ neighbourhood_group            <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
$ neighbourhood                  <chr> "Historic Anacostia", "Edgewood, Bloomi…
$ latitude                       <dbl> 38.86339, 38.91195, 38.88719, 38.90585,…
$ longitude                      <dbl> -76.98889, -77.00456, -76.99472, -76.94…
$ room_type                      <chr> "Private room", "Private room", "Privat…
$ price                          <dbl> 60, 63, 128, 64, NA, 74, 85, 52, 125, 5…
$ minimum_nights                 <dbl> 31, 1, 4, 30, 50, 31, 31, 31, 30, 31, 3…
$ number_of_reviews              <dbl> 84, 534, 64, 102, 96, 91, 415, 120, 38,…
$ last_review                    <dttm> 2023-08-30, 2025-02-19, 2025-01-30, 20…
$ reviews_per_month              <dbl> 0.48, 2.77, 0.33, 0.54, 0.51, 0.50, 2.2…
$ calculated_host_listings_count <dbl> 1, 5, 2, 2, 1, 27, 4, 4, 1, 4, 4, 2, 11…
$ availability_365               <dbl> 1, 349, 352, 179, 158, 310, 194, 218, 3…
$ number_of_reviews_ltm          <dbl> 0, 38, 6, 0, 0, 0, 3, 3, 0, 2, 2, 0, 1,…
$ license                        <chr> NA, "Hosted License: 5007242201001033",…
new_table1 <- select(df,room_type,price,minimum_nights)
new_table1
# A tibble: 6,257 × 3
   room_type       price minimum_nights
   <chr>           <dbl>          <dbl>
 1 Private room       60             31
 2 Private room       63              1
 3 Private room      128              4
 4 Private room       64             30
 5 Entire home/apt    NA             50
 6 Entire home/apt    74             31
 7 Entire home/apt    85             31
 8 Private room       52             31
 9 Entire home/apt   125             30
10 Private room       52             31
# ℹ 6,247 more rows
new_table1 %>%
  summarise(missing_prices = sum(is.na(price)))
# A tibble: 1 × 1
  missing_prices
           <int>
1           1488
new_table1
# A tibble: 6,257 × 3
   room_type       price minimum_nights
   <chr>           <dbl>          <dbl>
 1 Private room       60             31
 2 Private room       63              1
 3 Private room      128              4
 4 Private room       64             30
 5 Entire home/apt    NA             50
 6 Entire home/apt    74             31
 7 Entire home/apt    85             31
 8 Private room       52             31
 9 Entire home/apt   125             30
10 Private room       52             31
# ℹ 6,247 more rows
airbnb_clean <- df%>%
  select(price,number_of_reviews, room_type)%>%
filter(!is.na(price)&!is.na(number_of_reviews)&!is.na(room_type))
airbnb_clean
# A tibble: 4,769 × 3
   price number_of_reviews room_type      
   <dbl>             <dbl> <chr>          
 1    60                84 Private room   
 2    63               534 Private room   
 3   128                64 Private room   
 4    64               102 Private room   
 5    74                91 Entire home/apt
 6    85               415 Entire home/apt
 7    52               120 Private room   
 8   125                38 Entire home/apt
 9    52               106 Private room   
10    52               109 Private room   
# ℹ 4,759 more rows
ggplot(airbnb_clean, aes(x = number_of_reviews, y = price, color = room_type))+
  labs(title = "Relationship Between Price and Number of Reviews",
  x = "Number of Reviews",
  y = "Price ($)",
  caption = "Source: Airbnb dataset provided",
  color= "Room Type")+
  geom_point()

# I removed the outliers because those listing price were above $4000 usd. In addition, according to our graph, shared rooms with a price of $4000 represents 0.02% of our data set.
airbnb_clean2 <- df%>%
  select(price,number_of_reviews, room_type)%>%
  filter(!is.na(price)&!is.na(number_of_reviews)&!is.na(room_type)&!(price>4000))
airbnb_clean2
# A tibble: 4,766 × 3
   price number_of_reviews room_type      
   <dbl>             <dbl> <chr>          
 1    60                84 Private room   
 2    63               534 Private room   
 3   128                64 Private room   
 4    64               102 Private room   
 5    74                91 Entire home/apt
 6    85               415 Entire home/apt
 7    52               120 Private room   
 8   125                38 Entire home/apt
 9    52               106 Private room   
10    52               109 Private room   
# ℹ 4,756 more rows
#we can see that our observations have been reduced only in 3 observations after removing the outliers
ggplot(airbnb_clean2, aes(x = number_of_reviews, y = price, color = room_type))+
  labs(title = "Relationship Between Price and Number of Reviews",
       x = "Number of Reviews",
       y = "Price ($)",
       caption = "Source: Airbnb dataset provided",
       color= "Room Type")+
  geom_point()

#The representation shows us that there is not a clear relationship between the price and the number of the reviews. 
neighbourhood_prices<-df%>%
  filter(!is.na(price),!is.na(neighbourhood))%>%
  group_by(neighbourhood)%>%
  summarise(average_price=mean(price),.groups="drop")
neighbourhood_prices
# A tibble: 39 × 2
   neighbourhood                                                   average_price
   <chr>                                                                   <dbl>
 1 Brightwood Park, Crestwood, Petworth                                    117. 
 2 Brookland, Brentwood, Langdon                                           130. 
 3 Capitol Hill, Lincoln Park                                              188. 
 4 Capitol View, Marshall Heights, Benning Heights                          97.6
 5 Cathedral Heights, McLean Gardens, Glover Park                          241. 
 6 Cleveland Park, Woodley Park, Massachusetts Avenue Heights, Wo…         151. 
 7 Colonial Village, Shepherd Park, North Portal Estates                   236. 
 8 Columbia Heights, Mt. Pleasant, Pleasant Plains, Park View              136. 
 9 Congress Heights, Bellevue, Washington Highlands                        134. 
10 Deanwood, Burrville, Grant Park, Lincoln Heights, Fairmont Hei…         114. 
# ℹ 29 more rows
#Since we cannot see a relation between prices and reviews, I decided to try to find out if there are a relation between the price and the neighborhood of the airbnb properties.
neighbourhood_prices<-neighbourhood_prices%>%
  mutate(short_neighbourhood=str_remove(neighbourhood,",.*"))
neighbourhood_prices
# A tibble: 39 × 3
   neighbourhood                               average_price short_neighbourhood
   <chr>                                               <dbl> <chr>              
 1 Brightwood Park, Crestwood, Petworth                117.  Brightwood Park    
 2 Brookland, Brentwood, Langdon                       130.  Brookland          
 3 Capitol Hill, Lincoln Park                          188.  Capitol Hill       
 4 Capitol View, Marshall Heights, Benning He…          97.6 Capitol View       
 5 Cathedral Heights, McLean Gardens, Glover …         241.  Cathedral Heights  
 6 Cleveland Park, Woodley Park, Massachusett…         151.  Cleveland Park     
 7 Colonial Village, Shepherd Park, North Por…         236.  Colonial Village   
 8 Columbia Heights, Mt. Pleasant, Pleasant P…         136.  Columbia Heights   
 9 Congress Heights, Bellevue, Washington Hig…         134.  Congress Heights   
10 Deanwood, Burrville, Grant Park, Lincoln H…         114.  Deanwood           
# ℹ 29 more rows
#We use the function remove to make the names of the neighbourhoods shorter.
ggplot(neighbourhood_prices,aes(x = short_neighbourhood, y = 1, fill = average_price)) +
  geom_tile() +
  labs(title = "Average Airbnb Price by Neighborhood",
    x = "Neighborhood",
    y = NULL,
    fill = "Average Price ($)",
    caption = "Source: Airbnb dataset provided for class") +
  scale_y_continuous(breaks = NULL) +
  theme(axis.text.x = element_text(angle = 90,hjust = 1,size = 7))

I created another visualization were light colors represents higher average Airbnb prices, and while colors become darker the prices becomes lower.