Reputation: 1809
I have a dataframe:
CityVsPrice=data.frame(City,Price)
City Price
New York 10000
New York 15000
New York 12000
Madison 800
Lodi 8000
Chico 9000
Redlands 200
I then wish to sort this list by price, giving me the top 3 cities for price. So ideally, New York would only show up once, followed by Chico, and then Lodi. Maybe another way to approach this is take the highest value for each city, and then sort descending and pick top 3. Any easy way to do so?
Thanks in advance!
Upvotes: 0
Views: 73
Reputation: 33488
There are ways to do this in base R concisely but here goes a solution that uses data.table
:
setDT(CityVsPrice)[, .(Price = max(Price)), by=City][order(-Price)][1:3]
City Price
1: New York 15000
2: Chico 9000
3: Lodi 8000
Upvotes: 1
Reputation: 1809
So, this finally worked:
Price=as.numeric(as.character(Price))
ByPrice<- data.frame(aggregate(Price ~ City, data = CityVsPrice, max))
Upvotes: 1
Reputation: 711
tidyverse does this very well, you will love it :)
library(dplyr)
data.frame(City,Price) %>%
group_by(City) %>%
top_n(1, Price) %>%
ungroup() %>%
top_n(3, Price)
Upvotes: 1