Creating an equal depth bin

Hi all,

I have a task where I am creating an equal depth bin. My goal is to come up with the following bin:

[3, 4), [5, 6] and [7, 8]
library(classInt)
quality <- c(3, 4, 5, 6, 7, 8)
df.wine <- data.frame(quality)
w <- classIntervals(df.wine$quality
                    , n=3
                    , style = "pretty"
                    )
w

But when I run my R code I get this

[2,4) [4,6) [6,8]

I request someone to point me in the right direction.

Thank you,

Ron

Can you supply us with your code and some sample data.

For some suggestions see:

1 Like

Hi Ron,

What you get is correct, it seems to me. R treats your vector as a numeric, so a continuous interval ranging from 3 to 8. The binning therefore is continuous. R returns is the interval splits, not where the elements of your vector go. Are you probably looking for following explanation (I used cut-function, not your package)?

class(quality)                                                                
# [1] "numeric"                                                               
cut(quality, breaks = c(3,4,6,8), include.lowest = TRUE)                      
# [1] [3,4] [3,4] (4,6] (4,6] (6,8] (6,8]                                     
# Levels: [3,4] (4,6] (6,8]                                                   
                                                                              
table(cut(quality, breaks = c(3,4,6,8), include.lowest = TRUE) )              
#                                                                             
# [3,4] (4,6] (6,8]                                                           
#     2     2     2                                                           
                                                                              
#use split to see the element-split                                           
f  <- as.factor(cut(quality, breaks = c(3,4,6,8), include.lowest = TRUE) )    
split(quality, f)                                                             
# $`[3,4]`                                                                    
# [1] 3 4                                                                     
#                                                                             
# $`(4,6]`                                                                    
# [1] 5 6                                                                     
#                                                                             
# $`(6,8]`                                                                    
# [1] 7 8                                                                     
#                                                                             

Otherwise, as @jrkrideau suggests, you need to provide more information for what you trying to achieve.

Hope this helps, JW

1 Like

Hi JW,

Thanks for the information. I will definitely do what JW suggested next time I ask a question.

The cut package does a better a job in making me understand what I was trying to achieve.

Thank you,

Ron

Hi Ron,

Glad to be of help. Note that cut is not a package, but a base R function, so always available! If you are looking to understand what is going on, looking into base R solutions for common operations, would be my general recommendation. Gives a feeling what is going on under the hood.

Welcome and have fun withR!

JW