【问题标题】:Create a new data.table from sub-tables with the same key columns从具有相同键列的子表创建一个新的 data.table
【发布时间】:2021-03-26 12:29:43
【问题描述】:

我有带列的数据

Time - AgentID -- SimulationID -- discountFactor -- displacement

对于每个by =.(AgentID,SimulationID,discountFactor),我要计算acf(displacement, doPlot=False)$acf,并将其输出到新的data.table dStat。但是由于acf(...)$acf的输出大小小于Time,所以不能直接定义新列,所以需要新建data.table。

我该怎么做?

我的整个数据集的一部分:

structure(list(Time = c(2.00099550677104, 2.00108827485947, 2.00108827485947, 
2.00108827485947, 2.00108827485947, 2.00108827485947, 2.00108827485947, 
2.00108827485947, 2.00108827485947, 2.00108827485947, 2.00108827485947
), AgentID = c(40L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), 
    SimulationID = c(2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L), discountFactor = c(0.2, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 
    0.8, 0.8, 0.8, 0.8), displacement = c(-4L, -7L, -1L, 6L, 
    -6L, 6L, -1L, -4L, 0L, 0L, -3L)), row.names = c(NA, -11L), class = c("data.table", 
"data.frame"), .internal.selfref = <pointer: 0x55a2cca3bde0>, sorted = "Time")

【问题讨论】:

    标签: r data.table


    【解决方案1】:

    像这样?

    dt[, .(acf = c(acf(displacement, plot = FALSE)$acf)), .(AgentID, SimulationID, discountFactor)]
    

    由于行在 by 上是唯一的,因此对您的示例数据并没有真正的作用。

    【讨论】:

      猜你喜欢
      • 2022-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-20
      • 2021-12-16
      • 2019-03-12
      • 2021-12-07
      相关资源
      最近更新 更多