且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

R-根据该行中另一列的特征选择一行

更新时间:2023-01-20 10:32:37

一个标准的 R 解决方案是:

A standard R solution would be:

aggregate(num~id, dat, max)

结果:

      id   num
1  27618 27620
2  28769 43984
3  31245 31247
4  31335 31337
5  40036 40039
6  40517 40519
7  41994 41997
8  42507 42510
9  43551 43554
10 44979 44982

您还可以使用 data.table 来执行此操作如下:

You can also use data.table to do this as follows:

require(data.table)
setDT(dat)[,.(num = max(num)),by=id]
setDF(dat) #making dat a normal data.frame again

这将返回每个id的最高值为num。

This will return the highest value per id as num.