Hi, I want to select a group of values determined by a value in a data table.
In particular, I would like to select all columns grouped by date and identifier for all positive values, where e == 1
id date e logret 7 2011-07-29 1 -0.0272275211 7 2011-07-29 2 0.0034229025 7 2011-07-29 3 0.0042622177 8 2011-07-29 1 0.0035662770 8 2011-07-29 2 -0.0015268474 8 2011-07-29 3 0.0013333333 7 2011-07-30 1 0.0044444444 7 2011-07-30 2 -0.0001111111 7 2011-07-30 3 0.0013333333
here all the elements for id 8 and the date 2011-07-29, and all id 7 elements for the date 2011-07-30 will be selected, because logret for e == 1 is> 0, where, like all id 7 elements in 2011-07-29 are ignored since the first logret (where e == 1) 0
Ans:
8 2011-07-29 1 0.0035662770 8 2011-07-29 2 -0.0015268474 8 2011-07-29 3 0.0013333333 7 2011-07-30 1 0.0044444444 7 2011-07-30 2 -0.0001111111 7 2011-07-30 3 0.0013333333
in sql I would use some kind of subtitle for this. I would:
1) Select the id and date where e=1 and logret > 0 2) Select * join on results of subselect
I think data.table can do this too, but it's hard for me to describe it in terms of data.table. In particular, I can repeat step 1, but I cannot complete part of the connection in step 2.
pos <- DT[e==1][logret > 0]
But can't join pos values back to my DT
source share