简体   繁体   English

使用计数找到前 5 个中位数 r

[英]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. id 喜欢显示数据框中社区的前 5 个中位数价格。 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!但是我不知道如何只显示前 5 名。非常感谢!

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:基础 R:

med_price <- aggregate(price ~ neighbourhood, edibnb, median)
med_price[order(-med_price$price), ][1:5, ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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