Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First element in a data.table aggregation

Tags:

r

data.table

I have a data.table of tick data, which I want to aggregate into seconds timeframe. While getting max, min and last is pretty straightforward:

data[, list(max(value), min(value), last(value)), by=time]

I am struggling to get the first datapoint which corresponds to a certain second timestamp. There is nothing in the manual. Is there an easy way to do it, like say, SQL TOP?

like image 364
flipper Avatar asked Jan 10 '23 11:01

flipper


2 Answers

I managed to find the solution. The query to get the first element is to just subset that column's first value using [:

data[, list(value[1], max(value), min(value), last(value)),by=time]

Maybe it helps someone.

like image 146
flipper Avatar answered Jan 20 '23 18:01

flipper


It seems that first is a valid aggregation.

foo <- data.table(x=1:10, y=11:20)
     x  y
 1:  1 11
 2:  2 12
 3:  3 13
 4:  4 14
 5:  5 15
 6:  6 16
 7:  7 17
 8:  8 18
 9:  9 19
10: 10 20

foo[, .(first(x), last(x))]

    V1 V2
1:  1 10
like image 26
teemoleen Avatar answered Jan 20 '23 18:01

teemoleen