I want to split a vector according to given cutoffs (or breaks) into bins in R.
This can be done as followed:
numbers <- 1:10
cutoffs <- c(0,2,6,7,10, Inf)
data.frame(data = numbers, bins = as.integer(cut(numbers, breaks = cutoffs, right = FALSE)))
This will result in the following:
data bins
1 1 1
2 2 2
3 3 2
4 4 2
5 5 2
6 6 3
7 7 4
8 8 4
9 9 4
10 10 5
However, what I want is for the lowest value to be in the highest bin and vice versa. So somehow I want to achieve the following instead:
data bins
1 1 5
2 2 4
3 3 4
4 4 4
5 5 4
6 6 3
7 7 2
8 8 2
9 9 2
10 10 1
I have tried many combinations with 'rev()' to reverse the sequence. But keep in mind I cannot just reverse the results of the earlier cut-command as they are asymmetrical.
I realise there might be a simple solution to this, but for some reason it keeps escaping me. Any suggestions in which direction I should look?
Since you wanted the left interval boundaries closed and wanted an integer result then findInterval
(rather than cut
) is the natural choice:
data.frame(data = numbers, bins = 6L - findInterval(numbers, vec = cutoffs))
data bins
1 1 5
2 2 4
3 3 4
4 4 4
5 5 4
6 6 3
7 7 2
8 8 2
9 9 2
10 10 1
>