Steef Gregor
Steef Gregor

Reputation: 544

R - using only some of the data.table keys

I have a data.table:

           time type   price       size
  1: 1396527597  ASK 436.000  5.4194370
  2: 1396527597  ASK 436.003  0.6464547
  3: 1396527597  ASK 436.004  0.2400000
  4: 1396527597  ASK 436.999  0.1000000
  5: 1396527597  ASK 437.000 10.9000000
 ---                                   
8843660: 1396544914  BID 445.600  0.0194000
8843661: 1396544914  BID 446.608  0.0712000
8843662: 1396544914  BID 446.608  0.0712000
8843663: 1396544914  BID 446.659  0.0500000
8843664: 1396544914  BID 446.659  0.0500000

keyed as follows:

 NAME         NROW      MB COLS                        KEY            
[1,] result  8,843,664 237 time,type,price,size        time,type,price

So if I want to to quickly search for some specific value, I need to provide always three keyes.

Say, that I want to find all values, where type = BID and price = 449.1?

Writing this:

result[J("BID", 449.1)]

of course ends with an error.

My question is, what is the sytax for searching only according to two keys when the table has three keys? Removing the not-used key is not an option.

Upvotes: 1

Views: 60

Answers (1)

ilir
ilir

Reputation: 3224

If you only have one value for the other two keys, you could pass all values of the first key:

result[J(unique(time), "BID", 449.1)]

If you have more values for the other keys, then take a look at CJ() which uses a Cartesian product of the values as key.

Upvotes: 3

Related Questions