简体   繁体   English

获取先前未包含的数字的 cut2 区间

[英]Obtain the cut2 interval for numbers not previously included

Actually, I have solved this question, but I have problems because the solution is in two steps, which are really separated between each other (the first step is inside a function and the second step is inside another; this would imply me to make H as an output).其实我已经解决了这个问题,但是我有问题,因为解决方案是分两步进行的,这两个步骤实际上是相互分离的(第一步在一个函数内,第二步在另一个函数内;这意味着我要使 H作为输出)。

First, the replicable example:首先是可复制的例子:

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

I would like to know a solution which can be done as:我想知道一个解决方案,可以这样做:

new_number %in% H

Give me your opinion.给我你的意见。

I (think) the request is for determination of the interval number for a new value relative to a factor vector constructed with cut2.我(认为)请求是确定相对于用 cut2 构造的因子向量的新值的区间数。 If that is what is needed then use as.numeric on a gsub construction of the first of the two cuts in each factor level:如果这是需要的,那么在每个因子级别的两个切割中的第一个的 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

I had never seen the onlycuts parameter used before, but it would make the code even easier, since the as.numeric( gsub(...)) calls would not be needed:我以前从未见过使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM