我有一个包含很多行的数据集,但我只选择了一些如下所示,并且只需要选择最早的SORT_DT,其余所有变量保持不变。
CUST_NO ID_NO SYMBOL AUTO_CREATE_DT CLASS_TYPE SORT_DT
1 107 10120 1 2014-05-12 G/L 2015-01-09
2 107 10120 1 2014-05-12 G/L 2015-11-10
3 107 10120 1 2014-05-12 G/L 2014-06-18
4 107 10120 1 2014-05-12 G/L 2014-05-12
5 107 10120 1 2014-05-12 G/L 2015-07-10
6 107 10120 1 2014-05-12 G/L 2015-10-09
7 107 10120 1 2014-05-12 G/L 2016-04-08
8 107 10120 1 2014-05-12 G/L 2016-01-08
9 107 10120 1 2014-05-12 G/L 2016-12-22
10 107 10120 1 2014-05-12 G/L 2017-01-13
11 107 10120 1 2014-05-12 G/L 2016-07-08
12 107 10120 1 2014-05-12 G/L 2017-04-14
13 107 10120 1 2014-05-12 G/L 2017-04-17
14 107 10120 1 2014-05-12 G/L 2016-08-31
15 107 10120 1 2014-05-12 G/L 2015-04-10
16 107 10120 1 2014-05-12 G/L 2016-12-22
我需要输出为
的形式 CUST_NO ID_NO SYMBOL AUTO_CREATE_DT CLASS_TYPE SORT_DT
1 107 10120 1 2014-05-12 G/L 2014-05-12
如果有人有解决方案,请告诉我。
我还添加了新的数据集
df <- fread("CUST_NO ID_NO SYMBOL AUTO_CREATE_DT CLASS_TYPE SORT_DT
107 10120 1 2014-05-12 G/L 2015-01-09
107 10120 1 2014-05-12 G/L 2015-11-10
107 10120 1 2014-05-12 G/L 2014-06-18
107 10120 1 2014-05-12 G/L 2014-05-13
107 10120 1 2014-05-12 G/L 2015-07-10
107 10120 1 2014-05-12 G/L 2015-10-09
107 10120 1 2014-05-12 G/L 2016-04-08
107 10120 1 2014-05-12 G/L 2016-01-08
107 10120 1 2014-05-12 G/L 2016-12-22
107 10120 1 2014-05-12 G/L 2017-01-13
107 10120 1 2014-05-12 G/L 2016-07-08
108 10120 1 2014-05-12 G/L 2017-04-14
108 10120 1 2014-05-12 G/L 2017-04-17
108 10120 1 2014-05-12 G/L 2016-08-31
108 10120 1 2014-05-12 G/L 2015-04-10
108 10120 1 2014-05-12 G/L 2016-12-22")
输出应该如下所示
CUST_NO ID_NO SYMBOL AUTO_CREATE_DT CLASS_TYPE SORT_DT
1 107 10120 1 2014-05-12 G/L 2014-05-13
2 108 10120 1 2014-05-12 G/L 2015-04-10
答案 0 :(得分:2)
试试这个:
aggregate(SORT_DT~.,min,data=df)
输出:
CUST_NO ID_NO SYMBOL AUTO_CREATE_DT CLASS_TYPE SORT_DT
1 107 10120 1 2014-05-12 G/L 2014-05-13
2 108 10120 1 2014-05-12 G/L 2015-04-10
答案 1 :(得分:1)
尝试aggregate
。
res <- aggregate(SORT_DT ~ CUST_NO + ID_NO + SYMBOL + AUTO_CREATE_DT + CLASS_TYPE, data = df, FUN = min)
res
CUST_NO ID_NO SYMBOL AUTO_CREATE_DT CLASS_TYPE SORT_DT
1 107 10120 1 2014-05-12 G/L 2014-05-13
2 108 10120 1 2014-05-12 G/L 2015-04-10