【发布时间】:2020-10-20 06:02:36
【问题描述】:
假设我下面有一个数据框
Date <- as.Date("2020-01-01") + 0:9
Ticker <- rep("KO-US", 10)
Daily_Ret <- c(0.00803, 0, 0, 0.00469, -0.00468, -0.0193, -0.0120, -0.0141, 0, 0)
Cumulative_Ret <- c(1.19, 1.19, 1.19, 1.20, 1.19, 1.17, 1.15, 1.14, 1.14, 1.14)
Daily_Cumulative_Ret <- tibble(Date, Ticker, Daily_Ret, Cumulative_Ret)
# A tibble: 10 x 4
Date Ticker Daily_Ret Cumulative_Ret
<date> <chr> <dbl> <dbl>
1 2020-01-01 KO-US 0.00803 1.19
2 2020-01-02 KO-US 0 1.19
3 2020-01-03 KO-US 0 1.19
4 2020-01-04 KO-US 0.00469 1.2
5 2020-01-05 KO-US -0.00468 1.19
6 2020-01-06 KO-US -0.0193 1.17
7 2020-01-07 KO-US -0.012 1.15
8 2020-01-08 KO-US -0.0141 1.14
9 2020-01-09 KO-US 0 1.14
10 2020-01-10 KO-US 0 1.14
我想在Cumulative_Ret 中的值 1.20(或我设置的任何阈值)之后将相应 Daily_Ret 列中的所有值替换为 0。我该怎么做(最好使用 dplyr)?我知道我可以使用filter(cumany(Cumulative_Ret >= 1.20)) 过滤之后的值,但不确定如何将Daily_Ret 列中的相应值替换为0。
最终结果将如下所示:
Date Ticker Daily_Ret Cumulative_Ret
<date> <chr> <dbl> <dbl>
1 2020-01-01 KO-US 0.00803 1.19
2 2020-01-02 KO-US 0 1.19
3 2020-01-03 KO-US 0 1.19
4 2020-01-04 KO-US 0.00469 1.2
5 2020-01-05 KO-US 0 1.19
6 2020-01-06 KO-US 0 1.17
7 2020-01-07 KO-US 0 1.15
8 2020-01-08 KO-US 0 1.14
9 2020-01-09 KO-US 0 1.14
10 2020-01-10 KO-US 0 1.14
提前致谢。
【问题讨论】: