[R] Removing row with smallest value, for a given factor
    David Winsemius 
    dwinsemius at comcast.net
       
    Sat Apr 23 16:02:05 CEST 2011
    
    
  
On Apr 23, 2011, at 9:05 AM, - - wrote:
> I have a table.
> First column is a date, second column is an index and other columns  
> contains some other values.
> I want to remove, for each date, the row with the smallest index (it  
> is not necessarily 1).
>
> ex: in the following table, I want to remove row 1 (2013-05-12 with  
> index 2) and row 8 (2013-05-13 with index 1)
>
>            day index values
> 1    2013-05-12    2  xxxx
> 2    2013-05-12    3  xxxx
> 3    2013-05-12    4  xxxx
> 4    2013-05-12    5  xxxx
> 5    2013-05-12    6  xxxx
> 6    2013-05-12    7  xxxx
> 7    2013-05-12    8  xxxx
> 8    2013-05-13    1  xxxx
> 9    2013-05-13    3  xxxx
> 10   2013-05-13    4  xxxx
> 11   2013-05-13    5  xxxx
> 12   2013-05-13    6  xxxx
> 13   2013-05-13    7  xxxx
> 14   2013-05-13    8  xxxx
> 15   2013-05-13    9  xxxx
> 16   2013-05-13   10  xxxx
> 17   2013-05-13   12  xxxx
>
Consider using ave and creating a logical vector that you then negate:
 > ave(dat$index, list(dat$day), FUN=function(x) x==min(x))
  [1] 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
dat[ -ave(dat$index, list(dat$day), FUN=function(x) x==min(x)), ]
--
David Winsemius, MD
West Hartford, CT
    
    
More information about the R-help
mailing list