【问题标题】:How to replace values in the rest of a column after certain threshold?如何在特定阈值后替换列其余部分的值?
【发布时间】: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 &gt;= 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

提前致谢。

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    您可以使用replacecumany 返回的逻辑值将1.2 之后的值更改为0。可能您还想为每个Ticker 执行此操作。

    library(dplyr)
    
    Daily_Cumulative_Ret %>%
      group_by(Ticker) %>%
      mutate(Daily_Ret = replace(Daily_Ret, 
                         lag(cumany(Cumulative_Ret >= 1.2), default = FALSE), 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
    

    【讨论】:

    • 谢谢,这行得通!如果我想添加另一个阈值,例如
    • 所以您的意思是在Cumulative_Ret &gt;= 1.2 Or Cumulative_Ret &lt;= 0.95 之后将所有值更改为0?
    • 是的,基本上我想做的是当Cumulative_Ret &gt;= 1.2 时,Daily_Ret 的其余值将为 0,或者当 Cumulative_Ret &lt; 0.95 时,Daily_Ret 的其余值将为 0。我有许多相同长数据格式的代码。
    • 我认为更改cumany 中的条件应该可以做到。 cumany(Cumulative_Ret &gt;= 1.2 | Cumulative_Ret &lt;= 0.95).
    【解决方案2】:

    这行得通吗:

    > library(dplyr)
    > Daily_Cumulative_Ret %>% group_by(Ticker) %>% 
    +   mutate(rowid = which(Cumulative_Ret >=1.2)) %>% 
    +     mutate(Daily_Ret = case_when(row_number() > rowid ~ 0, TRUE ~ Daily_Ret)) %>% select(-rowid)
    # A tibble: 10 x 4
    # Groups:   Ticker [1]
       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
    > 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 2021-05-18
      • 2021-04-14
      相关资源
      最近更新 更多