Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cut by Defined Interval

Tags:

r

Is there some way in R to cut by a defined interval without any breaks?

For example, if I want the values in the exact interval [1,10]; by default cut breaks this interval into smaller intervals.

like image 333
Barata Avatar asked Apr 21 '11 15:04

Barata


People also ask

What does cut () do in R?

The cut function in R allows you to cut data into bins and specify 'cut labels', so it is very useful to create a factor from a continuous variable.

What is the cut function?

The cut command removes the selected data from its original position, while the copy command creates a duplicate; in both cases the selected data is kept in temporary storage (the clipboard). The data from the clipboard is later inserted wherever a paste command is issued.


1 Answers

To cut unto pre-defined intervals, you can specify a vector of breaks using the breaks parameter.

Define some data:

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

Now cut x at 0, 10 and 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] 
like image 61
Andrie Avatar answered Sep 19 '22 06:09

Andrie