[R] Removing row with smallest value, for a given factor

Peter Ehlers ehlers at ucalgary.ca
Sat Apr 23 19:30:14 CEST 2011


On 2011-04-23 07:02, David Winsemius wrote:
>
> 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)), ]

ave() is one of those really handy functions, but I think
that you meant

  dat[ !ave(dat$index, list(dat$day), FUN=function(x) x==min(x)), ]

Here's another way, using the plyr package

  require(plyr)
  ddply(dat, .(day), .fun = function(x) subset(x, index != min(index)))

Peter Ehlers

> --
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list