R中的复杂算法,数据表使用以前的行值

R中的复杂算法,数据表使用以前的行值,r,data.table,R,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"), r

我的数据以下面给出的数据表的形式存在

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>)
结构(列表(atp=c(1,0,1,0,0,1),len=c(2,NA,3,NA, NA,1),inv=c(593823668640593745),GU=c(3694, 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=) 我需要形成4个新列csi_begin、csi_end、IRQ和csi_order。当atp=1时,csi_开始和csi_结束的值直接取决于inv和gu值

但当atp不等于1时,csi_开始和csi_结束取决于上一行的inv和gu值以及IRQ值 如果atp==1,则IRQ的值取决于该行的csi_顺序,否则其0和csi_顺序值取决于前两行的csi_开始值

我在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
}
}

lostsales将所需结果添加到示例中会非常有帮助,因为我在遵循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
库(data.table)
#示例数据:

dt您的数据中没有名为
IRQ
的列,但它出现在您的示例中/因为IRQ是根据CSi_order的值形成的,否则atp!=1好的,这是有道理的,但请看您的第一条
else
语句。您引用的
transit$IRQ
之前没有创建它(
transit$csi\u begin[i]是的,这是这里的问题。我无法理解如何在这里引用以前的值,因为csi\u begin您的评论不清楚。循环按定义“逐行”进行。您想引用的是什么?