r - R 中的复杂算法与 data.tables 使用先前的行值

标签 r data.table

我的数据采用下面给出的 data.table 形式

structure(list(atp = c(1, 0, 1, 0, 0, 1), len = c(2, NA, 3, NA, 
NA, 1), inv = c(593, 823, 668, 640, 593, 745), GU = c(36, 94, 
57, 105, 48, 67), RUTL = c(100, NA, 173, NA, NA, 7)), .Names = c("atp", 
"len", "inv", "GU", "RUTL"), row.names = c(NA, -6L), class = c("data.table", 
"data.frame"), .internal.selfref = <pointer: 0x0000000000320788>)

我需要形成 4 个新列 csi_begin、csi_end、IRQ 和 csi_order。当 atp=1 时 csi_begin 和 csi_end 的值直接取决于 inv 和 gu 值。

但是当atp不等于1时csi_begin和csi_end取决于inv和gu值以及上一行的IRQ值 如果 atp==1,则 IRQ 的值取决于该行的 csi_order,否则其 0 和 csi_order 值取决于前两行的 csi_begin 值。

我已经借助 for 循环编写了条件。 下面是给出的代码

lostsales<-function(transit)
{

if (transit$atp==1)
{
  transit$csi_begin[i]<-(transit$inv)[i]
  transit$csi_end[i]<-transit$csi_begin[i]-transit$GU[i]
}
else
{
  transit$csi_begin[i]<-(transit$inv)[i]+transit$IRQ[i-1]
  transit$csi_end[i]<-transit$csi_begin[i]-transit$GU[i]
}
if (transit$csi_begin[i-2]!= NA)
{
  transit$csi_order[i]<-transit$csi_begin[i-2]
}
else
  { transit$csi_order[i]<-0}
if (transit$atp==1)
{
  transit$IRQ[i]<-transit$csi_order[i]-transit$RUTL[i] 
}

else
{
  transit$IRQ[i]<-0
}
}

任何人都可以帮助我如何使用 setkeys 对 data.tables 进行高效循环吗?由于我的数据集非常大,我不能使用 for 循环,否则时间会非常长。

最佳答案

将期望的结果添加到您的示例中会非常有帮助,因为我在遵循 if/then 逻辑时遇到了问题。但我还是试了一下:

library(data.table)

# Example data:
dt <- structure(list(atp = c(1, 0, 1, 0, 0, 1), len = c(2, NA, 3, NA, NA, 1), inv = c(593, 823, 668, 640, 593, 745), GU = c(36, 94, 57, 105, 48, 67), RUTL = c(100, NA, 173, NA, NA, 7)), .Names = c("atp", "len", "inv", "GU", "RUTL"), row.names = c(NA, -6L), class = c("data.table", "data.frame"), .internal.selfref = "<pointer: 0x0000000000320788>")

# Add a row number:
dt[,rn:=.I]

# Use this function to get the value from a previous (shiftLen is negative) or future (shiftLen is positive) row:
rowShift <- function(x, shiftLen = 1L) {
  r <- (1L + shiftLen):(length(x) + shiftLen)
  r[r<1] <- NA
  return(x[r])
}

# My attempt to follow the seemingly circular if/then rules:
lostsales2 <- function(transit) {
  # If atp==1, set csi_begin to inv and csi_end to csi_begin - GU:
  transit[atp==1, `:=`(csi_begin=inv, csi_end=inv-GU)]

  # Set csi_order to the value of csi_begin from two rows prior:
  transit[, csi_order:=rowShift(csi_begin,-2)]

  # Set csi_order to 0 if csi_begin from two rows prior was NA
  transit[is.na(csi_order), csi_order:=0]

  # Initialize IRQ to 0
  transit[, IRQ:=0]

  # If ATP==1, set IRQ to csi_order - RUTL
  transit[atp==1, IRQ:=csi_order-RUTL]

  # If ATP!=1, set csi_begin to inv + IRQ value from previous row, and csi_end to csi_begin - GU
  transit[atp!=1, `:=`(csi_begin=inv+rowShift(IRQ,-1), csi_end=inv+rowShift(IRQ,-1)-GU)]
  return(transit)
}

lostsales2(dt)
##    atp len inv  GU RUTL rn csi_begin csi_end csi_order  IRQ
## 1:   1   2 593  36  100  1       593     557         0 -100
## 2:   0  NA 823  94   NA  2        NA      NA         0    0
## 3:   1   3 668  57  173  3       668     611       593  420
## 4:   0  NA 640 105   NA  4       640     535         0    0
## 5:   0  NA 593  48   NA  5       593     545       668    0
## 6:   1   1 745  67    7  6       745     678       640  633

此输出是否接近您的预期?

关于r - R 中的复杂算法与 data.tables 使用先前的行值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20656031/

相关文章:

r - 复杂热图突出显示特定行

r - 如何使用两个不同的标准对 data.table 执行两次聚合函数

r - 将字符串作为代码传递以使用 data.table 汇总多列

R - 有条件的索引位置

R:强制 data.table 计算所有交互

r - 有人可以解释在 data.table 中执行更新时,mult 是如何工作的吗(使用 .EACHI 和 mult)

r - data.table .. 用 j 中的函数表示法

r - 检查和可视化大型数据框中的间隙/空白和结构

r - 我无法在data.table中使用dtplyr做什么

r - 将字符串传递给 ggplot 函数