This question is the extension from here.
If my data has a column called Remark
:
ID Name Type Date Amount Remark
1 AAAA First 2009/7/20 100 Not want
1 AAAA First 2010/2/3 200 want ya
2 BBBB First 2015/3/10 250
2 CCC Second 2009/2/23 300 good
2 CCC Second 2010/1/25 400 OK Right123
2 CCC Third 2015/4/9 500
2 CCC Third 2016/6/25 700 Stackoverflow is awesome
I want my result to keep it when the Date
is max.
First, if i don't consider column Remark
, I can use max()
to get this:
dt[,.(Date = max(Date), Amount = sum(Amount)), by = .(ID, Name, Type)]
ID Name Type Date Amount
1: 1 AAAA First 2010-02-03 300
2: 2 BBBB First 2015-03-10 250
3: 2 CCC Second 2010-01-25 700
4: 2 CCC Third 2016-06-25 1200
However, how can I keep Remark.
ID Name Type Date Amount Remark
1: 1 AAAA First 2010-02-03 300 want ya
2: 2 BBBB First 2015-03-10 250
3: 2 CCC Second 2010-01-25 700 OK Right123
4: 2 CCC Third 2016-06-25 1200 Stackoverflow is awesome
Here is my data:
dt <- fread("
ID Name Type Date Amount Remark
1 AAAA First 2009/7/20 100 Not.want
1 AAAA First 2010/2/3 200 want.ya
2 BBBB First 2015/3/10 250
2 CCC Second 2009/2/23 300 good
2 CCC Second 2010/1/25 400 OK.Right123
2 CCC Third 2015/4/9 500
2 CCC Third 2016/6/25 700 Stackoverflow.is.awesome
")
dt$Date <- as.Date(dt$Date)