The cut function

I am trying to use the cut function in R to group a list of values between 620-290. I am using this line of code

mutate(group_no = cut(n,50))

The output is this. Is it possible to modify the range to be 1-100,101-200,201-300. The whole idea for this is to make it easy to represent the data in a graph which is 1024 long. Happy to try a different approach if anyone can suggest anything.

1 (330,336]   
 2 (310,316]  
 3 (303,310]   
 4 (336,343]   
 5 (297,303] 
 6 (343,349]   
 7 (323,330]   
 8 (316,323]    
 9 (290,297]    
10 (349,356]   
11 (613,620]    
12 (607,613]   

If the only goal is to create bins of equal size consider this code; I am using bin size 5, but 100 would work as well.

library(tidyverse)

binsize <- 5

asdf <- data.frame(n = seq(1:30)) %>%
  mutate(group = n %/% binsize)

Thanks! Can you define a start point of a group as I am getting a 0 for a group?

Thanks! This wouldn't really help me. I need to be able to show the range of groups as well. Thanks anyway.

In that case consider a label column:

library(tidyverse)

binsize <- 5

asdf <- data.frame(n = seq(1:30)) %>%
  mutate(group = n %/% binsize,
         label = paste(binsize * group + 1, '-', binsize * (group + 1)))
1 Like

That seems to work well. Thanks for your help.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.