Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.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 cut()-包括最低值_R - Fatal编程技术网

R cut()-包括最低值

R cut()-包括最低值,r,R,我想使用cut()中定义的中断来剪切数据: x=c(-10:10) 切割(x,c(-2,4,6,7)) [1] (-2,4] (-2,4] (-2,4] (-2,4] (-2,4] (-2,4] (4,6] (4,6] (6,7] [21] 级别:(-2,4](4,6)(6,7) 但是,我还想获得级别(最小值:-2]和(最大值)-7。在汽车套件的函数recode()中,可以使用“lo:”。是否有类似的东西可供剪切?x

我想使用
cut()
中定义的中断来剪切数据:

x=c(-10:10)
切割(x,c(-2,4,6,7))
[1]                            (-2,4] (-2,4] (-2,4] (-2,4] (-2,4] (-2,4] (4,6]  (4,6]  (6,7]       
[21]   
级别:(-2,4](4,6)(6,7)

但是,我还想获得级别
(最小值:-2]
(最大值)-7。在汽车套件的函数
recode()
中,可以使用“lo:”。是否有类似的东西可供剪切?

x我以前在填充
Inf
-Inf
时遇到问题(虽然这正是为什么我在这个时候不知道)所以一个更安全的解决方案可能是使用适当扩展的最小值和最大值:

x <- -10:10

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

# Levels: (-Inf,-2] (-2,4] (4,6] (6,7] (7, 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]
x切割(x,c(最小(x)-1,-2,4,6,7,最大(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]  
级别:(-11,-2](-2,4](4,6)(6,7)(7,11)

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

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
i您可以使用
min()
max()
评估间隔范围(如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]

我们还可以从软件包
cutr
中使用
smart\u-cut

# 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]
#devtools::安装_github(“moodymudskipper/cutr”)
图书馆(cutr)
x(My)
santoku
如有必要,套餐会自动延长休息时间:

library(santoku)

你不需要正面Inf@RJ前面的
+
:我知道;它只是用于说明目的。
# 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]