找到最常见的价值,但是当有领带时,请选择“整理”
因此,假设我有一些这样的数据:
ID value date
001 A 2015-12-06
001 A 2015-12-07
001 A 2015-12-08
002 B 2015-12-09
002 C 2015-12-10
003 A 2015-12-11
003 B 2015-12-12
002 B 2015-12-13
004 D 2015-12-13
004 R 2015-12-13
我想找到每个ID最常出现的value
。但是,当有领带时,请采取最新日期的价值。
预期输出:
ID value
001 A
002 B
003 B
004 R
在004
的情况下,您可能会注意到,在TIE期间有相同的日期和相同的ID。在这种情况下,您可以使用较低的排名。
So let's say I have some data like this:
ID value date
001 A 2015-12-06
001 A 2015-12-07
001 A 2015-12-08
002 B 2015-12-09
002 C 2015-12-10
003 A 2015-12-11
003 B 2015-12-12
002 B 2015-12-13
004 D 2015-12-13
004 R 2015-12-13
I want to find the value
that appears most frequently for each ID. But when there is a tie, take the most recent date's value.
Expected Output:
ID value
001 A
002 B
003 B
004 R
You may notice in the case of 004
there is the same date AND same ID during a tie. In this case, you can use the lower row entry.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用以下代码:
在2022-07-02创建的 reprex package (v2(v2)。 0.1)
You can use the following code:
Created on 2022-07-02 by the reprex package (v2.0.1)
更新,澄清后。请参阅评论:这是给出预期输出的版本:
第一个答案:
Note Group 0004有联系,没有最新日期,因此两个值都保存在数据框中:
Update, after clarification OP. See comments: Here is the version that gives the expected output:
First answer:
Note group 0004 has ties and no most recent date, therefore both values are kept in the dataframe: