DiamondJoe12
DiamondJoe12

Reputation: 1809

Dataframe: descending sorting not working

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

Answers (3)

s_baldur
s_baldur

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

DiamondJoe12
DiamondJoe12

Reputation: 1809

So, this finally worked:

Price=as.numeric(as.character(Price))

ByPrice<- data.frame(aggregate(Price ~ City, data = CityVsPrice, max))

Upvotes: 1

Selcuk Akbas
Selcuk Akbas

Reputation: 711

tidyverse does this very well, you will love it :)

https://dplyr.tidyverse.org/

library(dplyr)
data.frame(City,Price) %>% 
  group_by(City) %>% 
  top_n(1, Price) %>% 
  ungroup() %>% 
  top_n(3, Price)

Upvotes: 1

Related Questions