[R] Partial aggregate on sorted data

kees chaogai at duineveld.demon.nl
Wed Oct 24 20:19:39 CEST 2007


Yves,

why not:
aggregate(myDataframe$TargetValue,list(SomeFactor =
  myDataframe$SomeFactor),function(x) mean(x[x>quantile(x,.66)]))


Op Wed, 24 Oct 2007 15:30:10 +0200 schreef Yves Moisan  
<ymoisan at groupesm.com>:

>
> Hi All,
>
> I'm looking for ways to compute aggregate statistics (with the aggregate
> function) but with an option for sorting and selecting a subset of the  
> data
> frame.  For example, I have would like to turn this :
>
> aggregate(myDataframe$TargetValue,list(SomeFactor =
> myDataframe$SomeFactor),mean)
>
> into something like
>
> aggregate(myDataframe$TargetValue,list(SomeFactor =
> myDataframe$SomeFactor),mean, sort=DESCENDING, subset=0.33)
>
> where sort would sort TargetValue per factor level and subset would be  
> (for
> example) a value between 0 and 1.  The example above would give me the  
> mean
> for the top third of TargetValue per factor.
>
> Any way of doing this without having to use temporary variables to stuff  
> my
> vectors, use length(), etc ?



-- 
Gemaakt met Opera's revolutionaire e-mailprogramma:  
http://www.opera.com/mail/



More information about the R-help mailing list