I am new to R and I am trying to create bins, 3 bins to be exact, for data that I have to sort. I need the first bin to collect values on the interval (-inf, -3] and be called "Improved", the second bin to collect values on the interval (-3, 3) and called "None", and I need the third bin to collect values on the interval [3, inf) called "Worsened". I tried using the cut function to no avail since all examples that I've come across have the closed interval on only one side and my intervals contain one interval with no closed intervals and two intervals with closed intervals on different sides.
Asked
Active
Viewed 65 times
0
-
I find `case_when()` from the `dplyr` package easiest for this. – Phil Mar 30 '20 at 23:28
-
With some more searching across the internet, I came across the fancy_cut package that allowed me to very easily do what I needed to. – Nikola Kuzmanovic Mar 31 '20 at 01:06
1 Answers
0
This can be achieved by installing and using the "fancycut" package.
In my case, this code worked:
new_name <- fancycut(x, Improved = '(-Inf, -3]', None = '(-3, 3)', Worsened = '[3, Inf)')
The fancycut package allows one to quite intuitively create intervals for bins as well as give them labels. Easy solution!