【问题标题】:How can I compare a real number to a column in a tibble?如何将实数与小标题中的列进行比较?
【发布时间】:2020-06-25 16:58:01
【问题描述】:

我正在尝试使用runif() 生成一组随机概率,然后将其与小标题中的累积概率列表进行比较,以便我可以查找预期的市场回报。是否有一种比较随机概率的方法来比较随机概率,看看它是否等于或小于 CumulativeProb 列中的值之一。

CumulativeProb <- scan(text = '0.12, 0.52,0.77,0.92,1.00', sep = ',')
MarketReturn <- scan(text = '0.23,0.18,0.15,0.09,0.03', sep = ',')
df1 <- tibble::tibble(CumulativeProb)

【问题讨论】:

    标签: r dplyr tidyverse tibble


    【解决方案1】:

    我不确定以下是否是问题所要求的。

    下面的代码使用findInterval 来确定区间[0, 1] 中的数字向量在累积概率中的位置,并返回区间的索引。

    library(dplyr)
    
    df1 %>%
      mutate(MarketReturn = MarketReturn,
             Where = findInterval(MarketReturn, CumulativeProb, left.open = TRUE, rightmost.closed = TRUE) + 1L)
    ## A tibble: 5 x 3
    #  CumulativeProb MarketReturn Where
    #           <dbl>        <dbl> <int>
    #1           0.12         0.23     2
    #2           0.52         0.18     2
    #3           0.77         0.15     2
    #4           0.92         0.09     1
    #5           1            0.03     1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-24
      • 2014-10-19
      • 2015-09-29
      • 2023-03-15
      • 2021-05-18
      • 2010-09-06
      • 1970-01-01
      • 2020-12-30
      相关资源
      最近更新 更多