R 使用data.tables时循环
我有以下(代表性)数据集(实际数据集的一小部分) 换句话说,对于给定的R 使用data.tables时循环,r,data.table,tidyverse,R,Data.table,Tidyverse,我有以下(代表性)数据集(实际数据集的一小部分) 换句话说,对于给定的(时间,模拟ID,N,折扣因子),对于x中的所有不同AgentID,分别在i中查找AgentIDs,其i\u阶段是AgentID的i\u反相(来自x) 当然,上述代码中的语法不起作用,因此我正在寻找实现上述操作的方法 注:纯数据。首选表解决方案。如果我正确理解了您的问题: a[a,.(x.AgentID,i.AgentID,Time,SimulationID,N,discountFactor,x.i_antiPhase), o
(时间,模拟ID,N,折扣因子)
,对于x
中的所有不同AgentID
,分别在i
中查找AgentID
s,其i\u阶段
是AgentID
的i\u反相
(来自x
)
当然,上述代码中的语法不起作用,因此我正在寻找实现上述操作的方法
注:纯
数据。首选表解决方案。如果我正确理解了您的问题:
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,...
如果我正确理解了你的问题:
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,...
代码末尾有一个空的[]
是有原因的吗?原因是在最初的实现中,我使用了:=
,正如您所做的那样,在这个引用赋值中[]
允许显示结果。在最终的解决方案中不再需要它:我删除了它。代码末尾有空的[]
有什么原因吗?原因是在最初的实现中,我使用了:=
,正如您所做的那样,在这个引用赋值中[]
允许显示结果。在最终的解决方案中不再需要这个:我删除了它。
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,...