##Part 1 of assignment
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
#Pulling dataset
airbnb <- read.csv("~/Desktop/airbnb.csv")
# Rename the column
names(airbnb)[6] <- "Neighborhood"
Neighborhood<- airbnb$Neighborhood
#use a group by and summazie function, followed by %>% (pipe, meaning just do this in the next step)
average_price_by_neighborhood <- function(x) {
airbnb %>%
group_by(Neighborhood)%>%
summarize(avg_price = mean(price, na.rm=TRUE))
}
average_number_of_reviews <- function(x) {
airbnb %>%
group_by(Neighborhood)%>%
summarize(avg_reviews = mean(number_of_reviews, na.rm=TRUE))
}
#Calling our functions
average_price_by_neighborhood()
## # A tibble: 221 × 2
## Neighborhood avg_price
## <chr> <dbl>
## 1 Allerton 87.6
## 2 Arden Heights 67.2
## 3 Arrochar 115
## 4 Arverne 172.
## 5 Astoria 117.
## 6 Bath Beach 81.8
## 7 Battery Park City 368.
## 8 Bay Ridge 144.
## 9 Bay Terrace 142
## 10 Bay Terrace, Staten Island 102.
## # ℹ 211 more rows
average_number_of_reviews()
## # A tibble: 221 × 2
## Neighborhood avg_reviews
## <chr> <dbl>
## 1 Allerton 42.9
## 2 Arden Heights 7.75
## 3 Arrochar 14.6
## 4 Arverne 29.3
## 5 Astoria 21.5
## 6 Bath Beach 21.4
## 7 Battery Park City 8.29
## 8 Bay Ridge 18.6
## 9 Bay Terrace 41.5
## 10 Bay Terrace, Staten Island 1.5
## # ℹ 211 more rows
##Part 2 of assignment
total_review_summary <- function(x){
airbnb%>%
group_by(Neighborhood, room_type ) %>%
summarize(avg_price = mean(price, na.rm=TRUE) , avg_reviews = mean(number_of_reviews, na.rm=TRUE))
}
total_review_summary()
## `summarise()` has regrouped the output.
## ℹ Summaries were computed grouped by Neighborhood and room_type.
## ℹ Output is grouped by Neighborhood.
## ℹ Use `summarise(.groups = "drop_last")` to silence this message.
## ℹ Use `summarise(.by = c(Neighborhood, room_type))` for per-operation grouping
## (`?dplyr::dplyr_by`) instead.
## # A tibble: 540 × 4
## # Groups: Neighborhood [221]
## Neighborhood room_type avg_price avg_reviews
## <chr> <chr> <dbl> <dbl>
## 1 Allerton Entire home/apt 123. 54
## 2 Allerton Private room 65.8 36.1
## 3 Arden Heights Entire home/apt 76 8
## 4 Arden Heights Private room 41 7
## 5 Arrochar Entire home/apt 190. 16.9
## 6 Arrochar Private room 47.1 12.5
## 7 Arverne Entire home/apt 225. 26.9
## 8 Arverne Private room 87.7 35.7
## 9 Arverne Shared room 38 0
## 10 Astoria Entire home/apt 142. 22.1
## # ℹ 530 more rows
##EXTRA CODE, FOR DAIVIK'S REFERENCE
# Find the average price
#average_price <-function(price){mean(price, na.rm=TRUE)}
#paste("The Average Price is:", average_price(airbnb$price))
#Find the average # of reviews
#number_of_reviews <-function(number_of_reviews){mean(number_of_reviews, na.rm=TRUE)}
#paste("The Average # of Reviews is:", number_of_reviews(airbnb$number_of_reviews))