Find the number of instances for the maximum value for each unique element in R

I am trying to get this to work with a simple method.

Let's say there is a table for Cars Sold and with the name Car Model and Price Car was sold for

Eg.

CarName Price AcuraLegend 30000 AcuraTSX 40000 HondaCivic 20000 HondaCivic 22000 HondaCivic 22000 ToyotaCamry 18000 

and then another 2900 entries

I need to find the maximum price that each car sold, and the number of cars sold for the maximum amount. Thus, if we used the aforementioned framework, considering that the maximum price paid for HondaCivic in the entire data frame was 22,000, and only 2 cars were sold at this price, for HondaCivic I would have

 CarName MaxPricePaidForCar NumberofCarsSoldforMaxPrice HondaCivic 22000 2 

Now I managed to put this together with a rather tedious way of using tapply and merging, etc. etc.

Any suggestions on a simpler method would be very helpful,

+4
source share
3 answers

To do this for each unique type of vehicle, you can use ddply in the plyr package:

 ddply(carList,.(carName), .fun = summarise, maxPrice = max(Price), numCars = sum(Price == max(Price))) 
+6
source

Here is another approach using data.table . If your data frame is large and speed is a concern, this should give you approximately 4x speedup.

 library(data.table) carT = data.table(carList) carT[,list(maxPrice = max(Price), numCars = sum(Price == max(Price))),'carName'] 
+3
source

I really like the cast from the reshape package for these small tasks:

 cast(df, CarName ~., c(function(x) sum(x == max(x)),max)) 
+1
source

All Articles