我的数据格式如下
structure(list(Flag = c(1, 0, 0, 1, 0, 0, 1, 0), variable = c(3,
8, 6, 7, 1, 4, 3, 6), sale = c(26, 27, 61, 38, 79, 87, 81, 13
)), .Names = c("Flag", "variable", "sale"), row.names = c(NA,
-8L), class = "data.frame")
我想按如下方式创建输出
structure(list(Flag = c(1, 0, 0, 1, 0, 0, 1, 0), variable = c(3,
8, 6, 7, 1, 4, 3, 6), sale = c(26, 27, 61, 38, 79, 87, 81, 13
), begin = c(3, -23, -50, 7, -31, -70, 3, -78), end = c(-23,
-50, -111, -31, -70, -151, -78, -91)), .Names = c("Flag", "variable",
"sale", "begin", "end"), row.names = c(NA, -8L), class = "data.frame")
其中ne列的开始和结束基于以下algorathim
if flag=1 then
begin=variable;
end=variable-sale;
----------
else
begin=lag(end) ( i.e the previous value of end variable)
end= lag(end)-sale
我想要的是当flag为1时,“begin”的值等于“variable”值,“end”值是“variable-sale”值。 对于其他人而言,begin的值是前一行“end”值,而“end”值是(begin-sales)值 任何人都可以帮我解决如何在R中实现这个目标吗?
答案 0 :(得分:3)
我认为您提供的示例输出不正确,但我会尝试以下操作:
beginEnd <- by(indf, cumsum(indf$Flag), FUN = function(x) {
out <- Reduce("-", c(x[, "variable"][1], x[, "sale"]), accumulate = TRUE)
cbind(begin = head(out, -1),
end = tail(out, -1))
})
cbind(indf, do.call(rbind, beginEnd))
# Flag variable sale begin end
# 1 1 3 26 3 -23
# 2 0 8 27 -23 -50
# 3 0 6 61 -50 -111
# 4 1 7 38 7 -31
# 5 0 1 79 -31 -110
# 6 0 4 87 -110 -197
# 7 1 3 81 3 -78
# 8 0 6 13 -78 -91