[R] Removing row with smallest value, for a given factor
David Winsemius
dwinsemius at comcast.net
Sat Apr 23 20:53:32 CEST 2011
On Apr 23, 2011, at 1:30 PM, Peter Ehlers wrote:
> 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)), ]
Yes, that is what I should have answered. Somehow I thought that
because it was numeric I could use "-" but that would only be correct
if it returned line numbers. I'm not sure why it should return numeric
rather logical, but the help page does say the value will be numeric,
so I shouldn't be surprised, I suppose.
>
> 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.
>
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list