Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何将实数与TIBLE中的列进行比较?_R_Dplyr_Tidyverse_Tibble - Fatal编程技术网

R 如何将实数与TIBLE中的列进行比较?

R 如何将实数与TIBLE中的列进行比较?,r,dplyr,tidyverse,tibble,R,Dplyr,Tidyverse,Tibble,我试图使用runif()生成一组随机概率,然后将其与TIBLE中的累积概率列表进行比较,以便查找预期的市场回报。是否有一种简单的方法来比较随机概率,看看它是否等于或小于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 = ',') df

我试图使用
runif()
生成一组随机概率,然后将其与TIBLE中的累积概率列表进行比较,以便查找预期的市场回报。是否有一种简单的方法来比较随机概率,看看它是否等于或小于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)

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

下面的代码使用
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
库(dplyr)
df1%>%
突变(MarketReturn=MarketReturn,
其中=findInterval(MarketReturn,CumulativeProb,left.open=TRUE,rightmost.closed=TRUE)+1L)
##一个tibble:5x3
#CumulativeProb MarketReturn在哪里
#                    
#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