Split a vector into chunks

后端 未结 20 1904
时光说笑
时光说笑 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 01:58

    split(x,matrix(1:n,n,length(x))[1:length(x)])

    perhaps this is more clear, but the same idea:
    split(x,rep(1:n, ceiling(length(x)/n),length.out = length(x)))

    if you want it ordered,throw a sort around it

提交回复
热议问题