Reputation: 27
structure(list(price = c(80, 115, 46, 32, 100, 71, 175, 150,
139, 190, 85, 64, 79, 60, 55, 70, 120, 50, 280, 75), neighbourhood = c("New Town",
"Southside", NA, "Leith", "Southside", "Old Town", "New Town",
"West End", "Leith", "West End", "New Town", "Leith", "Leith",
NA, "Haymarket", "Morningside", "New Town", "Leith", "Newington",
"Leith")), row.names = c(NA, -20L), class = c("tbl_df", "tbl",
"data.frame"))
not sure if this is the right way to display the data frame if its wrong please tell me a better way. id like to display the top 5 median prices for the neighbourhoods in the data frame. I have displayed a couple of the rows in the data frame here but there are more. the code I used to get the median of each neighbourhood was:
edibnb %>%
count(neighbourhood, sort = TRUE)
however i dont know how to display just the top 5. Thanks very much!
Upvotes: 1
Views: 551
Reputation: 33488
This is what you are looking for?
edibnb %>%
group_by(neighbourhood) %>%
summarise(med_price = median(price), .groups = "drop") %>%
top_n(n = 5, med_price)
neighbourhood med_price
<chr> <dbl>
1 New Town 102.
2 Newington 280
3 Old Town 71
4 Southside 108.
5 West End 170
Base R:
med_price <- aggregate(price ~ neighbourhood, edibnb, median)
med_price[order(-med_price$price), ][1:5, ]
Upvotes: 3