我有一个包含日期和值的数据框,并且想要只有正数的cumsum和只有负数的一个。日期有时多次具有相同的日期,然后缺少几天(没有值=没有行)
首先,我刚刚测试了一笔累计金额。这些是累积的,但不是按日期排序:
df$cumsum <- cumsum(df$values)
# or
df$cumsum <- ave(df$values, FUN=cumsum)
# Should cumulate by date but did not in right order
df$cumsum <- cumsum(df[order(df$date, df$values), "values"])
最后找到了一个解决方案,它按照我的意愿完成了第一步(不是我想在数据帧中完成但是完成工作):
dt <- data.table(df)
dt[order(date), cumsum := cumsum(values)]
很好,但每次尝试过滤值&gt; 0没有成功。最后,我对数据进行了子集化并获得了结果,但这并不是我想要的。
dt.pos <- data.table(subset(df, values> 0))
dt.pos[order(date), cumsum := cumsum(values)]
dt.neg <- data.table(subset(df, values < 0))
dt.neg[order(date), cumsum := cumsum(values)]
我正在寻找像Python等价物一样简单的东西(带有序数据框):
df["cumsum_pos"] = df["values"][df["values"] > 0].cumsum()
df["cumsum_neg"] = df["values"][df["values"] < 0].cumsum()
/编辑
df <- data.frame(date = as.Date(c("2016-12-08", "2016-12-07", "2016-12-05", "2017-01-05",
"2017-01-10", "2017-01-11", "2017-01-11")),
values = c(10, -10, 5, 5, -7, 8, 8))
# just the cumsum
# expected output = c(5, -5, 5, 10, 3, 11, 19)
df$cumsum <- cumsum(df$values)
# output = c(10, 0, 5, 10, 3, 11, 19)
df$cumsum <- ave(df$values, FUN=cumsum)
# output = c(10, 0, 5, 10, 3, 11, 19)
df$cumsum <- cumsum(df[order(df$date, df$values), "values"])
# output = c(5, -5, 5, 10, 3, 11, 19) correct in this example
# doesn't work with dates in a different order 2016-12-31, 2016-12-30, ... 2015-12-31, 2015-12-30
# Now for just the positives
# expected output = c(10, 0, 5, 15, 15, 23, 31)
df$cumsum.pos[df$values > 0] <- cumsum(df[order(df$date, df$values), "values"][df$values > 0])
# output = c(5, NA, 15, 20, NA, 28, 36)
# And then the same with just the negatives
/编辑
尼古拉斯评论没有产生正确的输出
df<-df[order(df$date),]
# values = c(5, -10, 10, 5, -7, 8, 8)
# expected output = c(5, 5, 15, 20, 20, 28, 36)
df$cumsum<-ave(df$values,df$values>0,FUN=cumsum)
# output = c(5, -10, 15, 20, -17, 28, 36)
答案 0 :(得分:1)
你可以使用它。
library(data.table)
df <- as.data.table(df)
# Order by date
df <- df[order(date)]
# Perform the cumsum for positives and negatives separately
df[, expected := cumsum(values), by = sign(values)]
# Just for the negatives, get the previous positive value
df[, expected := ifelse(values > 0, expected, c(0, expected[-.N]))]
print(df)
date values expected
1: 2016-12-05 5 5
2: 2016-12-07 -10 5
3: 2016-12-08 10 15
4: 2017-01-05 5 20
5: 2017-01-10 -7 20
6: 2017-01-11 8 28
7: 2017-01-11 8 36
请注意,如果有多个连续的负值,则必须重复该操作。例如,如果您的数据框是这样的:
df <- data.frame(date = as.Date(c("2016-12-08", "2016-12-07", "2016-12-05", "2017-01-05","2017-01-10", "2017-01-10", "2017-01-11", "2017-01-11")),
values = c(10, -10, 5, 5, -7, -15, 8, 8))
单次执行上述代码会产生以下输出:
date values expected
1: 2016-12-05 5 5
2: 2016-12-07 -10 5
3: 2016-12-08 10 15
4: 2017-01-05 5 20
5: 2017-01-10 -7 20
6: 2017-01-10 -15 -17
7: 2017-01-11 8 28
8: 2017-01-11 8 36
值-17会出错。为了避免此问题,您可以重复此过程,直到没有任何负值。所以完整的代码将是:
df <- df[order(date)]
df[, expected := cumsum(values), by = sign(values)]
# If there are negative values, repeat the process
while(length(which(df$expected < 0))){
df[, expected := ifelse(values > 0, expected, c(0, expected[-.N]))]
}
print(df)
date values expected
1: 2016-12-05 5 5
2: 2016-12-07 -10 5
3: 2016-12-08 10 15
4: 2017-01-05 5 20
5: 2017-01-10 -7 20
6: 2017-01-10 -15 20
7: 2017-01-11 8 28
8: 2017-01-11 8 36