2

I am very new to R and I'm looking for a solution on How to create non overlapping class internal.

I did tried searching the stack overflow but unfortunately didn't find any answers.

Here is what i want and what i did:

x <- c(1,2,3,4,5,6,7,8,9)

i want - "1-3, 4-6 and 7-9" but what i am getting is - "1-3, 3-5, 5-7 and so on"

I used the below:

check <- c(1,2,3,4,5,6,7,8,9)
Check1 <- classIntervals(check, n=3, style='jenks')

Please note that i don't want to use the lengthy nested if conditions which is very in-efficient.

any help would be greatly appreciated.

Regards, Sagar Gupta

Biruk Abebe
  • 2,235
  • 1
  • 13
  • 24
Sagar.G
  • 21
  • 1
  • 1
    One method is to use `cut`: like so `cut(x, breaks=c(0,3,6,10))`. See if the `classIntervals` function has a breaks argument. Since it isn't part of base R and you didn't mention the package name, I can't check. – lmo May 22 '16 at 12:46
  • Hi Imo, first of all - Thank you very much for your response. I used cut as well, but if I am not wrong "breaks" will not work in this case. In the example you mentioned above, the results will be (0,3] (3,6] (6,10]. If you notice, 3 and 6 overlaps in the interval. FYI - I am using "classInt" package . Please let me know if there is a work around. Thanks so much. – Sagar.G May 22 '16 at 15:54
  • I am not sure what you mean by "3 and 6 overlaps in the interval." Could you clarify this? – lmo May 22 '16 at 19:22
  • Sure Imo, if i have to plot number 3 and 6, they both have two buckets for each. eg. 3 can go in 1-3 and 3-6, similarly 6 can go in 3-6 and 6-9. – Sagar.G May 23 '16 at 06:34
  • 1
    If you do not want overlaps, (0,3] (3,6] (6,10] is the correct outcome. For example, (0, 3] says the interval from 0 to 3, not including 0, but including 3. Analogously, (3, 6] does not include 3, but does include 6. You can see this by entering `cut(1:10, breaks=c(0,3,6,10))` in the console. `1:10` are the integers between 1 and 10. The first 3, {1, 2, 3} are grouped into (0, 3], while the next three {4, 5, 6} are grouped into the second category (3, 6]. I hope this helps. – lmo May 23 '16 at 11:34
  • Hi Imo, Thank you for the clarification. Also, is it possible to rename/label the classes in one go. I can rename it in later, but doing that will not be efficient. Appreciate your help! – Sagar.G May 24 '16 at 13:44
  • It sure is. you can do with with `factor`, which has a labels argument. You might create a temp vector and play with it to make sure you get the labels that you want. To check out the current ordering, use `levels`. – lmo May 24 '16 at 13:50

0 Answers0