R - вырезать по определенному интервалу - PullRequest
29 голосов
/ 21 апреля 2011

Есть ли какой-нибудь способ в R разрезать на определенный интервал без перерывов?

Например, если я хочу значения в точном интервале [1,10]; по умолчанию cut разбивает этот интервал на меньшие интервалы.

Ответы [ 2 ]

39 голосов
/ 21 апреля 2011

Чтобы сократить заранее заданные интервалы, вы можете указать вектор разрывов с помощью параметра breaks.

Определить некоторые данные:

x <- sample(0:20, 100, replace=TRUE)
x

Теперь вырезать x в 0,10 и 20:

cut(x, breaks=c(0, 10, 20), include.lowest=TRUE)

  [1] (10,20] [0,10]  [0,10]  (10,20] (10,20] (10,20] [0,10]  (10,20] (10,20]
 [10] (10,20] [0,10]  (10,20] (10,20] (10,20] [0,10]  (10,20] [0,10]  [0,10] 
 [19] [0,10]  (10,20] [0,10]  [0,10]  [0,10]  (10,20] [0,10]  (10,20] (10,20]
 [28] (10,20] (10,20] [0,10]  [0,10]  [0,10]  [0,10]  (10,20] [0,10]  [0,10] 
 [37] [0,10]  [0,10]  (10,20] (10,20] (10,20] (10,20] [0,10]  (10,20] [0,10] 
 [46] (10,20] [0,10]  (10,20] (10,20] [0,10]  [0,10]  (10,20] (10,20] (10,20]
 [55] [0,10]  [0,10]  (10,20] [0,10]  [0,10]  [0,10]  [0,10]  (10,20] (10,20]
 [64] (10,20] [0,10]  [0,10]  (10,20] (10,20] (10,20] (10,20] (10,20] (10,20]
 [73] (10,20] [0,10]  [0,10]  [0,10]  (10,20] [0,10]  (10,20] [0,10]  (10,20]
 [82] [0,10]  [0,10]  (10,20] [0,10]  [0,10]  [0,10]  (10,20] (10,20] [0,10] 
 [91] [0,10]  [0,10]  (10,20] (10,20] [0,10]  [0,10]  [0,10]  [0,10]  (10,20]
[100] (10,20]
Levels: [0,10] (10,20]
14 голосов
/ 21 апреля 2011

Как то так?Перерывы на каждые 0,2 с 0 до 1.

> a <- runif(100)
> cut(a, seq(from = 0, to = 1, by = 0.2))
  [1] (0,0.2]   (0.8,1]   (0.8,1]   (0.6,0.8] (0.6,0.8] (0,0.2]   (0.6,0.8]
  [8] (0.2,0.4] (0.8,1]   (0.4,0.6] (0.8,1]   (0.4,0.6] (0.8,1]   (0.6,0.8]
 [15] (0.8,1]   (0,0.2]   (0.8,1]   (0.8,1]   (0.6,0.8] (0.6,0.8] (0.2,0.4]
 [22] (0.4,0.6] (0.6,0.8] (0.2,0.4] (0.6,0.8] (0.6,0.8] (0.6,0.8] (0,0.2]  
 [29] (0,0.2]   (0.2,0.4] (0,0.2]   (0,0.2]   (0,0.2]   (0,0.2]   (0,0.2]  
 [36] (0.6,0.8] (0.2,0.4] (0.6,0.8] (0.6,0.8] (0.8,1]   (0.2,0.4] (0.4,0.6]
 [43] (0.4,0.6] (0.6,0.8] (0.2,0.4] (0.6,0.8] (0.6,0.8] (0.6,0.8] (0.4,0.6]
 [50] (0.6,0.8] (0.6,0.8] (0,0.2]   (0.2,0.4] (0.8,1]   (0.8,1]   (0.8,1]  
 [57] (0.6,0.8] (0.2,0.4] (0.2,0.4] (0,0.2]   (0.8,1]   (0.8,1]   (0.2,0.4]
 [64] (0.8,1]   (0.2,0.4] (0.4,0.6] (0.8,1]   (0,0.2]   (0.4,0.6] (0,0.2]  
 [71] (0.4,0.6] (0.8,1]   (0.6,0.8] (0.4,0.6] (0,0.2]   (0.2,0.4] (0.4,0.6]
 [78] (0,0.2]   (0.2,0.4] (0.8,1]   (0,0.2]   (0.4,0.6] (0.8,1]   (0,0.2]  
 [85] (0,0.2]   (0.2,0.4] (0.2,0.4] (0.4,0.6] (0.8,1]   (0.2,0.4] (0,0.2]  
 [92] (0.6,0.8] (0.2,0.4] (0.2,0.4] (0.8,1]   (0.2,0.4] (0.4,0.6] (0,0.2]  
 [99] (0,0.2]   (0,0.2]  
Levels: (0,0.2] (0.2,0.4] (0.4,0.6] (0.6,0.8] (0.8,1]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...