[英]Obtain the cut2 interval for numbers not previously included
其实我已经解决了这个问题,但是我有问题,因为解决方案是分两步进行的,这两个步骤实际上是相互分离的(第一步在一个函数内,第二步在另一个函数内;这意味着我要使 H作为输出)。
首先是可复制的例子:
RN = rnorm(n=1000,10,20)
H = cut2(RN,g=4,onlycuts=FALSE) # Step 1: The intervals are generated
H2= cut2(RN,g=4,onlycuts=TRUE) # Step 1: (This would be useful if Step 1 and 2 were not separated)
new_number = 10.53 # Step 2: New number
interval_new_number = cut2(new_number,cuts=H) # Step 2: Interval for new number
我想知道一个解决方案,可以这样做:
new_number %in% H
给我你的意见。
我(认为)请求是确定相对于用 cut2 构造的因子向量的新值的区间数。 如果这是需要的,那么在每个因子级别的两个切割中的第一个的 gsub 构造上使用 as.numeric :
H = cut2(RN,g=4,onlycuts=FALSE)
attributes(H)
#----
$class
[1] "factor"
$levels
[1] "[-66.7,-2.4)" "[ -2.4,10.3)" "[ 10.3,23.7)" "[ 23.7,75.9]"
findInterval( 10.53, as.numeric( gsub( "\\[|\\,.+$","", levels(H) ) ) )
[1] 3
我以前从未见过使用onlycuts
参数,但它会使代码更容易,因为as.numeric( gsub(...))
调用:
> (H2 = cut2(RN,g=4,onlycuts=TRUE) )
[1] -66.687208 -2.397688 10.334926 23.659386 75.887076
> findInterval( 10.53, H2 )
[1] 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.