使用 Dplyr 总结带有约束的数据框

Summarize dataframe with a constraint using Dplyr

我有一个类似于下面的数据框:

data <- data.frame(x = c("0", "2", "8", "1", "7", "10", "15", "14", "13", "11"),
                   y = c("11", "5", "14", "9", "13", "7", "4", "0", "12", "8"),
                   act_x = c("Short", "Buy", "Short", "Buy", "Short", "Buy", "Short", "Buy", "Short", "Buy"),
                   act_y = c("Buy", "Short", "Buy", "Short", "Buy", "Short", "Buy", "Short", "Buy", "Short"))

我希望根据对 x 和 y 采取的操作为 x 创建一个利润列,为 y 创建一个利润列。结果应如下所示:

res <- data.frame(data,
                  prof_x = c(NA, -2, 6, 7, 6, -3, 5, 1, -1, 2),
                  prof_y = c(NA, -6, -9, -5, -4, -6, 3, -4, -12, -4))

例如,从第 0 天(第一行)开始,我做空 x 并买入 y。相应的价格在第 1 天移动和结算(第二行)。 x 的利润是 0-2=-2(因为我做空了 x),y 的利润是 5-11=-6(因为我买了 y)。等等...

有没有一种友好的方式在 Dplyr 管道中实现它?在管道之外有人有什么建议吗?在此先感谢您的指导。

使用 lagmutate 的基于 dplyr 的解决方案可以实现为:

library(dplyr)
data %>% mutate(x = as.numeric(x), y = as.numeric(y)) %>%
  mutate(prof_x = ifelse(act_x == "Buy", lag(x)-x, x-lag(x))) %>%
  mutate(prof_y = ifelse(act_y == "Buy", lag(y)-y, y-lag(y)))
# 
# x  y act_x act_y prof_x prof_y
# 1   0 11 Short   Buy     NA     NA
# 2   2  5   Buy Short     -2     -6
# 3   8 14 Short   Buy      6     -9
# 4   1  9   Buy Short      7     -5
# 5   7 13 Short   Buy      6     -4
# 6  10  7   Buy Short     -3     -6
# 7  15  4 Short   Buy      5      3
# 8  14  0   Buy Short      1     -4
# 9  13 12 Short   Buy     -1    -12
# 10 11  8   Buy Short      2     -4

数据:

data <- data.frame(x = c("0", "2", "8", "1", "7", "10", "15", "14", "13", "11"),
        y = c("11", "5", "14", "9", "13", "7", "4", "0", "12", "8"),
        act_x = c("Short", "Buy", "Short", "Buy", "Short", "Buy", "Short", "Buy", "Short", "Buy"),
        act_y = c("Buy", "Short", "Buy", "Short", "Buy", "Short", "Buy", "Short", "Buy", "Short"),
        stringsAsFactors = FALSE)