Search code examples
rcut

R: Is cut the right function to do this?


I'm currently struggling with cut...I don't know if there's another function for this.

I have big table with values and a matrix or vector with thresholds.

Let's say I have a matrix containing the thresholds 0.6, 0.8, 1.0, 1.2, 1.4 I want to find out for a value (i.e. 0.9) in which sector this value falls. Basically it's a "grading" System. A Value <= 0.6 gets a 5, <= 0.8 and > 0.6 gets a 4 etc. So I want to write only this value (5, 4, 3 etc.) into the resulting table.

Ok here's the code I have so far:

cut(1.2, breaks=c(0.6,0.8,1.0,1.2,1.4), labels(5,4,3,2,1))

But this doesn't work yet..with the labels I actually don't know how many I have to insert there since I always get an error that the lenghts of the vectors are different. Without the labels parameter I still don't get it to work correctly. It still outputs all different segments and not only the one the value is in I guess...


Solution

  • cut should be the correct function, but you're doing things wrong.

    First, there are typos in your code. labels = c(...) would be the correct version.

    Second, think about what you're doing: creating intervals. How many? Try cut without the labels to see:

    cut(1.2, breaks=c(0.6,0.8,1.0,1.2,1.4))
    # [1] (1,1.2]
    # Levels: (0.6,0.8] (0.8,1] (1,1.2] (1.2,1.4]
    

    There are only 4 levels created the way you're doing it, so you only need to provide 4 labels (or redefine your break points).