【发布时间】:2021-08-24 20:38:51
【问题描述】:
我有以下(代表性)数据集(实际数据集的一小部分)
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>)
其中 i_phase,i_antiPhase == +/-1。
我想做的是这样的:
a[a, antiPhaseAgents:=list(i.AgentID[i_phase==x.i_antiPhase]), on=.(Time,SimulationID,N,SimulationID), by=.(x.AgentID,Time,SimulationID,N,discountFactor)]
换句话说,对于给定的(Time,SimulationID, N, discountFactor),对于x 中的所有不同AgentIDs,分别在i 中找到AgentIDs,其i_phase 是@987654330 的i_antiPhase @(来自x)考虑。
当然,上面代码中的语法是行不通的,所以我正在寻找实现上述操作的方法。
注意:首选纯data.table 解决方案。
【问题讨论】:
标签: r data.table tidyverse