+ Reply to Thread
Results 1 to 6 of 6

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

  1. #1
    TS Contributor
    Points: 41,667, Level: 100
    Level completed: 0%, Points required for next Level: 0
    gianmarco's Avatar
    Location
    Italy
    Posts
    1,381
    Thanks
    237
    Thanked 305 Times in 229 Posts

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




    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
    http://cainarchaeology.weebly.com/

  2. #2
    Devorador de queso
    Points: 97,539, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Posting AwardCommunity AwardDiscussion EnderFrequent PosterActivity Award
    Dason's Avatar
    Location
    Tampa, FL
    Posts
    12,987
    Thanks
    309
    Thanked 2,640 Times in 2,255 Posts

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

    Sounds like you want the `cut` function
    I don't have emotions and sometimes that makes me very sad.

  3. The Following User Says Thank You to Dason For This Useful Post:

    gianmarco (09-15-2017)

  4. #3
    TS Contributor
    Points: 41,667, Level: 100
    Level completed: 0%, Points required for next Level: 0
    gianmarco's Avatar
    Location
    Italy
    Posts
    1,381
    Thanks
    237
    Thanked 305 Times in 229 Posts

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

    Thnaks Dason, I will look into that...
    http://cainarchaeology.weebly.com/

  5. #4
    TS Contributor
    Points: 12,501, Level: 73
    Level completed: 13%, Points required for next Level: 349
    rogojel's Avatar
    Location
    I work in Europe, live in Hungary
    Posts
    1,491
    Thanks
    162
    Thanked 334 Times in 314 Posts

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

    hi,
    maybe the dplyr function between, with a bunch of ifelse-s?

    regards

  6. #5
    Devorador de queso
    Points: 97,539, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Posting AwardCommunity AwardDiscussion EnderFrequent PosterActivity Award
    Dason's Avatar
    Location
    Tampa, FL
    Posts
    12,987
    Thanks
    309
    Thanked 2,640 Times in 2,255 Posts

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

    Quote Originally Posted by rogojel View Post
    hi,
    maybe the dplyr function between, with a bunch of ifelse-s?

    regards
    Sounds like too much work when cut will do it all hassle free
    I don't have emotions and sometimes that makes me very sad.

  7. #6
    TS Contributor
    Points: 41,667, Level: 100
    Level completed: 0%, Points required for next Level: 0
    gianmarco's Avatar
    Location
    Italy
    Posts
    1,381
    Thanks
    237
    Thanked 305 Times in 229 Posts

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


    Ok. Just for the record:
    the following did the trick

    Code: 
    cut(df$values, jenks.brks$brks, labels=FALSE, include.lowest=TRUE)
    http://cainarchaeology.weebly.com/

+ Reply to Thread

           




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts






Advertise on Talk Stats