Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Number of Occurences for Maximum Value for each unique item in R

Tags:

r

I am trying to get this working by some simple method.

Say, there is a table for Cars Sold and with the name of the Car Model and the Price the Car was sold for

Eg.,

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

and then 2900 more entries

What I need is to find the maximum price each car was sold for and the number of that type of car sold for the maximum amount. So, if we were to use the above dataframe, assuming that the max price paid for HondaCivic in the entire dataframe was 22000, and only 2 cars were sold for this price, for HondaCivic I would have,

CarName     MaxPricePaidForCar         NumberofCarsSoldforMaxPrice
HondaCivic      22000                  2 

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

Any suggestions on a simpler method would be very helpful,

like image 869
xbsd Avatar asked Dec 28 '22 15:12

xbsd


2 Answers

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

ddply(carList,.(carName), .fun = summarise, maxPrice = max(Price), 
                                            numCars = sum(Price == max(Price)))
like image 179
joran Avatar answered Dec 31 '22 03:12

joran


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

library(data.table)
carT = data.table(carList)
carT[,list(maxPrice = max(Price), numCars = sum(Price == max(Price))),'carName']
like image 33
Ramnath Avatar answered Dec 31 '22 02:12

Ramnath