Reputation: 1035
I have the following (representative) dataset (a very small portion of the actual dataset)
structure(list(Time = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1), AgentID = 1:40, State = c(59L, 28L, 84L, 11L,
5L, 8L, 14L, 71L, 47L, 7L, 84L, 95L, 91L, 92L, 99L, 34L, 70L,
37L, 55L, 96L, 46L, 38L, 71L, 2L, 61L, 13L, 73L, 26L, 44L, 59L,
52L, 53L, 42L, 66L, 23L, 11L, 42L, 77L, 38L, 48L), Action = c(-1L,
-1L, 1L, -1L, 1L, 1L, 1L, -1L, -1L, -1L, -1L, 1L, 1L, 1L, 1L,
-1L, 1L, 1L, -1L, -1L, 1L, 1L, -1L, 1L, 1L, -1L, -1L, 1L, -1L,
-1L, 1L, -1L, -1L, 1L, 1L, -1L, -1L, 1L, 1L, 1L), N = c(40L,
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L,
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L,
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L
), SimulationID = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), discountFactor = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), i_phase = c(1L,
-1L, -1L, 1L, 1L, -1L, 1L, 1L, -1L, 1L, 1L, 1L, 1L, 1L, 1L, -1L,
-1L, 1L, 1L, 1L, 1L, -1L, -1L, 1L, -1L, -1L, -1L, -1L, -1L, -1L,
1L, -1L, -1L, -1L, -1L, 1L, -1L, 1L, 1L, -1L), i_antiPhase = c(-1,
1, 1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, 1, 1, -1,
-1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1,
-1, -1, 1), totalCount = c(40L, 40L, 40L, 40L, 40L, 40L, 40L,
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L,
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L,
40L, 40L, 40L, 40L, 40L, 40L, 40L), phaseCount = c(20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L),
phaseCountVar = c(1.24104938271605, 1.15579357351509, 1.15579357351509,
1.24104938271605, 1.24104938271605, 1.15579357351509, 1.24104938271605,
1.24104938271605, 1.15579357351509, 1.24104938271605, 1.24104938271605,
1.24104938271605, 1.24104938271605, 1.24104938271605, 1.24104938271605,
1.15579357351509, 1.15579357351509, 1.24104938271605, 1.24104938271605,
1.24104938271605, 1.24104938271605, 1.15579357351509, 1.15579357351509,
1.24104938271605, 1.15579357351509, 1.15579357351509, 1.15579357351509,
1.15579357351509, 1.15579357351509, 1.15579357351509, 1.24104938271605,
1.15579357351509, 1.15579357351509, 1.15579357351509, 1.15579357351509,
1.24104938271605, 1.15579357351509, 1.24104938271605, 1.24104938271605,
1.15579357351509)), row.names = c(NA, -40L), class = c("data.table",
"data.frame"), .internal.selfref = <pointer: 0x5619bf50c210>)
where i_phase, i_antiPhase == +/-1.
What I want to do is something like:
a[a, antiPhaseAgents:=list(i.AgentID[i_phase==x.i_antiPhase]), on=.(Time,SimulationID,N,SimulationID), by=.(x.AgentID,Time,SimulationID,N,discountFactor)]
In other words, for a given (Time,SimulationID, N, discountFactor)
, for all different AgentID
s in x
, seperately, find AgentID
s in i
whose i_phase
is THE i_antiPhase
of the AgentID
(from x
) in consideration.
Of course, the syntax in the above code doesn't work, so I'm looking for ways to implement the above operation.
Note: Pure data.table
solutions are preferred.
Upvotes: 0
Views: 55
Reputation: 41220
If I understood your question correctly:
a[a,.(x.AgentID,i.AgentID,Time,SimulationID,N,discountFactor,x.i_antiPhase), on=.(Time,SimulationID,N, i_phase=i_antiPhase),allow.cartesian=T][
,.(antiPhaseAgents=list(i.AgentID)),by=.(x.AgentID,Time,SimulationID,N,discountFactor)]
x.AgentID Time SimulationID N discountFactor antiPhaseAgents
1: 2 1 1 40 0 1, 4, 5, 7, 8,10,...
2: 3 1 1 40 0 1, 4, 5, 7, 8,10,...
3: 6 1 1 40 0 1, 4, 5, 7, 8,10,...
4: 9 1 1 40 0 1, 4, 5, 7, 8,10,...
5: 16 1 1 40 0 1, 4, 5, 7, 8,10,...
6: 17 1 1 40 0 1, 4, 5, 7, 8,10,...
Upvotes: 2