flipper
flipper

Reputation: 521

First element in a data.table aggregation

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?

Upvotes: 4

Views: 2374

Answers (2)

teemoleen
teemoleen

Reputation: 118

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

Upvotes: 3

flipper
flipper

Reputation: 521

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.

Upvotes: 6

Related Questions