6

我想使用定义的中断来削减我的数据cut()

x = c(-10:10)

cut(x, c(-2,4,6,7))

[1] <NA>   <NA>   <NA>   <NA>   <NA>   <NA>   <NA>   <NA>   <NA>   (-2,4] (-2,4] (-2,4] (-2,4] (-2,4] (-2,4] (4,6]  (4,6]  (6,7]  <NA>   <NA>  
[21] <NA>  
Levels: (-2,4] (4,6] (6,7]

但是,我也想获得水平(minimum:-2](7:maximum]. 在 car-package 的功能recode()中可以使用“lo:”。是否有类似的东西可用于切割?

4

6 回答 6

9
x <- -10:10

cut(x, c(-Inf, -2, 4, 6, 7, +Inf))

# Levels: (-Inf,-2] (-2,4] (4,6] (6,7] (7, Inf]
于 2012-09-03T09:29:59.860 回答
6

您可以使用min()andmax()来评估区间范围(如 Gavin 所述)并设置include.lowest = TRUE以确保最小值(此处:-10)是区间的一部分。

输入:

x = c(-10:10)

cut(x, c(min(x),-2,4,6,7,max(x)), include.lowest = TRUE)

输出:

 [1] [-10,-2] [-10,-2] [-10,-2] [-10,-2] [-10,-2] [-10,-2] [-10,-2] [-10,-2] [-10,-2] (-2,4]  
[11] (-2,4]   (-2,4]   (-2,4]   (-2,4]   (-2,4]   (4,6]    (4,6]    (6,7]    (7,10]   (7,10]  
[21] (7,10]  
Levels: [-10,-2] (-2,4] (4,6] (6,7] (7,10]
于 2014-09-22T12:50:04.653 回答
6

findInterval是答案。

i <- findInterval(x, c(-2,4,6,7))

cbind(x, i)

        x i
 [1,] -10 0
 [2,]  -9 0
 [3,]  -8 0
 [4,]  -7 0
 [5,]  -6 0
 [6,]  -5 0
 [7,]  -4 0
 [8,]  -3 0
 [9,]  -2 1
[10,]  -1 1
[11,]   0 1
[12,]   1 1
[13,]   2 1
[14,]   3 1
[15,]   4 2
[16,]   5 2
[17,]   6 3
[18,]   7 4
[19,]   8 4
[20,]   9 4
[21,]  10 4
于 2012-09-03T13:35:08.417 回答
2

Inf我在使用&之前遇到了填充问题-Inf(尽管这正是为什么在这个时间逃脱了我)所以一个更安全的解决方案可能是用适当扩展的最小值和最大值来填充:

x <- c(-10:10)
cut(x, c(min(x) -1 , -2, 4, 6, 7, max(x) + 1))

R> x <- c(-10:10)
R> cut(x, c(min(x) -1 , -2, 4, 6, 7, max(x) + 1))
 [1] (-11,-2] (-11,-2] (-11,-2] (-11,-2] (-11,-2] (-11,-2] (-11,-2] (-11,-2]
 [9] (-11,-2] (-2,4]   (-2,4]   (-2,4]   (-2,4]   (-2,4]   (-2,4]   (4,6]   
[17] (4,6]    (6,7]    (7,11]   (7,11]   (7,11]  
Levels: (-11,-2] (-2,4] (4,6] (6,7] (7,11]

但在大多数情况下,Sven 的答案/解决方案就足够了。

于 2012-09-03T09:45:59.100 回答
0

We can also use smart_cut from package cutr :

# devtools::install_github("moodymudskipper/cutr")
library(cutr)

x <- -10:10
smart_cut(x, c(-2, 4, 6, 7), closed="right")

#  [1] [-10,-2] [-10,-2] [-10,-2] [-10,-2] [-10,-2] [-10,-2] [-10,-2] [-10,-2] [-10,-2] (-2,4]   (-2,4]   (-2,4]   (-2,4]   (-2,4]  
# [15] (-2,4]   (4,6]    (4,6]    7        (7,10]   (7,10]   (7,10]  
# Levels: [-10,-2] < (-2,4] < (4,6] < 7 < (7,10]

It has an expand argument set to TRUE by default, setting it to FALSE makes it work like base::cut.

more on cutr and smart_cut

于 2018-10-05T23:09:33.927 回答
0

(My) santoku package automatically extends breaks if necessary:

library(santoku)
x <- c(-10:10)

chop(x, c(-2, 4, 6, 7))

##  [1] [-10, -2) [-10, -2) [-10, -2) [-10, -2) [-10, -2) [-10, -2) [-10, -2) [-10, -2) [-2, 4)  
## [10] [-2, 4)   [-2, 4)   [-2, 4)   [-2, 4)   [-2, 4)   [4, 6)    [4, 6)    [6, 7)    [7, 10]  
## [19] [7, 10]   [7, 10]   [7, 10]  
## Levels: [-10, -2) [-2, 4) [4, 6) [6, 7) [7, 10]

You can control this behaviour using the extend argument to chop().

于 2021-04-28T11:38:36.537 回答