Eric
Eric

Reputation: 111

How to use cut in R to divide a vector?

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.

Upvotes: 0

Views: 61

Answers (1)

Jon Spring
Jon Spring

Reputation: 66765

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.

Upvotes: 1

Related Questions