Split a vector into chunks

后端 未结 20 1894
时光说笑
时光说笑 2020-11-22 01:10

I have to split a vector into n chunks of equal size in R. I couldn\'t find any base function to do that. Also Google didn\'t get me anywhere. Here is what I came up with so

20条回答
  •  借酒劲吻你
    2020-11-22 02:12

    You could combine the split/cut, as suggested by mdsummer, with quantile to create even groups:

    split(x,cut(x,quantile(x,(0:n)/n), include.lowest=TRUE, labels=FALSE))
    

    This gives the same result for your example, but not for skewed variables.

提交回复
热议问题