简体   繁体   中英

using count to find top 5 median r

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!

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, ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM