R 中的复杂算法,其中 data.tables 使用前几行值

2023-12-15

我的数据采用下面给出的 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

这个输出是否接近您的预期?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

R 中的复杂算法,其中 data.tables 使用前几行值 的相关文章

随机推荐