The right answer obviously depends on the REQUIREMENTS and they may not have 
been fully stated.

This is a bit like finding the mode of a set of numbers. The most frequent 
value may not be as representative of the data as the mean or even the median 
for some purposes, as well as other measures of central tendency.

What does Rich want? Choices can include getting the entire row where the value 
happens first, or maybe even the last or ALL of them as a group.

Be that as it may, if you return all of them, as a mini-data.frame, it is easy 
enough to then choose to keep them all, or just the first by subsetting  it to 
get row 1, or even to get the last row by applying nrow() to the result.

I can think of many ways you want an answer including beyond this, some kind of 
blending of the other columns you get back to make some kind of composite row 
from all the rows that matched.

Without clear requirements, there often is no right answer.

-----Original Message-----
From: R-help <r-help-boun...@r-project.org> On Behalf Of Rui Barradas
Sent: Saturday, December 4, 2021 2:34 AM
To: Bert Gunter <bgunter.4...@gmail.com>
Cc: R-help <r-help@r-project.org>
Subject: Re: [R] Find tibble row with maximum recorded value

Hello,

You're right, I carelessly coded this.

which.max returns the index to the first maximum of a vector, while the 
comparison of a vector with its max() returns an index to all vector elements.

Às 23:27 de 03/12/21, Bert Gunter escreveu:
> Perhaps you meant to point this out, but the cfs[which.max(cfs)] and 
> cfs == ... are not the same:
> 
>> x <- rep(1:2,3)
>> x
> [1] 1 2 1 2 1 2
>> x[which.max(x)]
> [1] 2
>> x[x==max(x)]
> [1] 2 2 2
> 
> So maybe your point is: which does the OP want (in case there are 
> repeated maxes)? I suspect the == forms, but ...?
> 
> Bert Gunter
> 
> On Fri, Dec 3, 2021 at 2:56 PM Rui Barradas <ruipbarra...@sapo.pt> wrote:
>>
>> Hello,
>>
>> Inline.
>>
>> Às 22:08 de 03/12/21, Rich Shepard escreveu:
>>> On Fri, 3 Dec 2021, Rich Shepard wrote:
>>>
>>>> I find solutions when the data_frame is grouped, but none when it's not.
>>>
>>> Thanks, Bert. ?which.max confirmed that's all I need to find the 
>>> maximum value.
>>>
>>> Now I need to read more than ?filter to learn why I'm not getting 
>>> the relevant row with:
>>>> which.max(pdx_disc$cfs)
>>> [1] 8054
>>
>> This is the *index* for which cfs is the first maximum, not the 
>> maximum value itself.
>>
>>>
>>>> filter(pdx_disc, cfs == 8054)
>>
>> Therefore, you probably want any of

Should be "one of", not "any of"

Rui Barradas

>>
>>
>> filter(pdx_disc, cfs == cfs[8054])
>>
>> filter(pdx_disc, cfs == cfs[which.max(cfs)])
>>
>> filter(pdx_disc, cfs == max(cfs))    # I find this one better, simpler
>>
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>>
>>> # A tibble: 0 × 9
>>> # … with 9 variables: site_nbr <chr>, year <int>, mon <int>, day <int>,
>>> #   hr <dbl>, min <dbl>, tz <chr>, cfs <dbl>, sampdt <dttm>
>>>
>>> Regards,
>>>
>>> Rich
>>>
>>> ______________________________________________
>>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 
>>> 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.
>>
>> ______________________________________________
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 
>> 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.

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 
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.

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Reply via email to