How to add class labels for each value in a dataframe according to class breaks?

gianmarco

TS Contributor
#1
Hello,
I have a dataframe whose column $value stores some values I want to break into (say) three classes using the Jenks natural breaks method.

These are the (fictional) data:
Code:
df <- structure(list(values = c(11.0623420127031, 9.76686023702213, 
7.71567132998525, 10.1394056832603, 7.86601277747135, 9.2316598536534, 
8.28684276210926, 11.2445841256445, 9.93279208342987, 9.57945228099518, 
7.80901741794944, 12.1874175909243, 13.4053921107078, 14.3200495778918, 
9.75073471476137, 10.4107978132571, 4.26519405598576, 9.78073020870928, 
12.7952932938997, 8.82001672618872)), .Names = "values", row.names = c(NA, 
-20L), class = "data.frame")

      values
1  11.062342
2   9.766860
3   7.715671
4  10.139406
5   7.866013
6   9.231660
7   8.286843
8  11.244584
9   9.932792
10  9.579452
11  7.809017
12 12.187418
13 13.405392
14 14.320050
15  9.750735
16 10.410798
17  4.265194
18  9.780730
19 12.795293
20  8.820017
With the following code and with the use of the 'classIntervals' function (out from the classInt package), I can easily get the class breaks:

Code:
jenks.brks <- classIntervals(df$values,3)

jenks.brks$brks

[1]  4.265194  9.347591 10.320334 14.320050

Now, what I am after is trying to add to the original dataframe a new column containing a value (a sort of label, in other words) corresponding to the class each value
is actually falling into. Any idea about how that can be accomplished?

Best
Gm