Search code examples
rbreakcut

R: function cut


Here is my data:

>my.cut <- cut(my.variable, breaks = c(-Inf, -0.5, -0.25, -0.1, 0, 0.02, 0.05, 0.15, 0.3, 0.5, 1, Inf), right = FALSE)

>levels(my.cut)
    "[-Inf,-0.5)"  "[-0.5,-0.25)"  "[-0.25,-0.1)"  "[-0.1,0)"  "[0,0.02)"  "[0.02,0.05)"  "[0.05,0.15)"  "[0.15,0.3)"  "[0.3,0.5)"  "[0.5,1)"  "[1, Inf)"

Expected result:

>levels(my.cut)
    "[-Inf,-0.5)"  "[-0.5,-0.25)" "[-0.25,-0.1)" "[-0.1,0)"  "0"  "(0,0.02)" "[0.02,0.05)"  "[0.05,0.15)"  "[0.15,0.3)"  "[0.3,0.5)"   "[0.5,1)"      "[1, Inf)" 

In the expected result, there is single figure 0 which I only want to choose the my.variable==0, but with the formula of the my.cut, there is no single 0 cause breaks can only be used for interval. So how could I do?

Hope to get your answer soon! Thanks!


Solution

  • You could explicitly put each value into a group. This is more flexible, but also a lot more verbose.

    One way of doing this could be to define a bespoke cut function and then apply it to every element of your vector.

    my.variable <- rnorm(100)
    
    bespoke_cut <- function(value){
    
      if (value < 0.1) return('[-Inf, 0.1)')
    
      if (value < 0)   return('[0.1, 0)')
    
      if (value == 0)  return('0')
    
      return('(0, Inf]')
    }
    
    my.cut <- sapply(my.variable, bespoke_cut)
    
    my.cut <- factor(my.cut)
    

    I've only done a few of the groupings you wanted, but I think it should be apparent how to add extra groups.