Home > Software engineering >  How to use cut in R to divide a vector?
How to use cut in R to divide a vector?

Time:05-17

I heard I'm supposed to use the cut function in R to divide data into equal parts, but it doesn't seem to be as easy as

   which(cut(1:1000,3)==1)

My current solution is

t<-cut(1:1000,3)
which(match(t,levels(t))==1)

I don't believe this to be the best solution.

CodePudding user response:

s <- 1:1000
t <- split(s, cut(s, 3))

will give you a list with three groups, each having 1/3 of the sequence. Then you would use t[1] to get the first group, for instance.

  •  Tags:  
  • r cut
  • Related