带中断的切割如何在 R 中工作

How does cut with breaks work in R

我正在尝试了解 cut 如何划分和创建间隔;尝试了 ?cut 但无法弄清楚 r 中的 cut 是如何工作的。
这是我的问题:

set.seed(111)
data1 <- seq(1,10, by=1)
data1 
[1]  1  2  3  4  5  6  7  8  9 10
data1cut<- cut(data1, breaks = c(0,1,2,3,5,7,8,10), labels = FALSE)
data1cut
[1] 1 2 3 4 4 5 5 6 7 7

1。为什么 data1cut 结果中没有包含 8,9,10?
2. 为什么summary(data1)summary(data1cut)产生不同的结果?

summary(data1)
Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
1.00    3.25    5.50    5.50    7.75   10.00 

summary(data1cut)
Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
1.00    3.25    4.50    4.40    5.75    7.00  

我应该如何更好地使用 cut 以便我可以根据 summary 的结果创建说 4 bins (data1)?

bin1 [1 -3.25]
bin2 (3.25 -5.50]
bin3 (5.50 -7.75]
bin4 (7.75 -10] 

谢谢。

cut 在您的示例中将向量拆分为以下部分: 0-1 (1); 1-2 (2); 2-3 (3); 3-5 (4); 5-7 (5); 7-8 (6); 8-10 (7)

括号中的数字是 cut 根据提供的 breaks 值分配给每个容器的默认标签。

cut 默认不包括下限。如果你想改变它,那么你需要在 include.lowest 参数中指定它。

  1. 您没有分配标签,并且此函数中的默认参数为 FALSE,因此使用级别代码的整数向量(在括号中)代替。

  2. summary(data1) 是原始数据的摘要,summary(data1cut) 是您拆分的摘要。

您可以使用以下方式获得所需的拆分:

data2cut<- 
  cut(data1, breaks = c(1, 3.25, 5.50, 7.75, 10),
      labels = c("1-3.25", "3.25-5.50", "5.50-7.75", "7.75-10"),
      include.lowest = TRUE)

结果如下:

> data2cut

 [1] 1-3.25    1-3.25    1-3.25    3.25-5.50 3.25-5.50 5.50-7.75 5.50-7.75 7.75-10   7.75-10  
[10] 7.75-10  
Levels: 1-3.25 3.25-5.50 5.50-7.75 7.75-10

我希望现在已经清楚了。