开发者

Find Number of Occurences for Maximum Value for each unique item in 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,


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)))


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']


I quite like cast from the reshape package for these little tasks:

cast(df, CarName ~., c(function(x) sum(x == max(x)),max))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜