簡體   English   中英

獲取先前未包含的數字的 cut2 區間

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM