问题描述
我正在研究一个简化的大循环,以突出该问题。 在数据集“ DFloopSub”中,对于我正在研究的每只动物(“ IDTruie”),我都有几行。
目标是为每行计算2个新变量(“ QuantiteModif”和“ Reste”),知道“ QuantiteModif”(在“ l”行)需要考虑上一行的“ Reste”值(l-1)。 循环无法正常工作:“ Reste”列正确,但“ QuantiteModif”列未考虑“ Reste”(ifelse语句中的“ a”),而是仅取值“ 0”。您能帮我找出为什么它不起作用吗?
for(i in unique(DFloopSub[,"IDTruie"])) {
a <- 6 # define the inital value for "Reste"
DFloopSub$line <- 1:nrow(DFloopSub) # include line number
for (l in unique(DFloopSub[,"line"])){
DFloopSub$QuantiteModif <- ifelse(DFloopSub$JourEntree == DFloopSub$JourSortie,DFloopSub$Quantite,a) # need to use "Reste" at (l-1)
DFloopSub$Reste <- ifelse(DFloopSub$JourEntree == DFloopSub$JourSortie,DFloopSub$Quantite - DFloopSub$QuantiteModif)
a <- DFloopSub$Reste[l]
#print(a)
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)