Split a vector into chunks

后端 未结 20 1905
时光说笑
时光说笑 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:51

    Yet another possibility is the splitIndices function from package parallel:

    library(parallel)
    splitIndices(20, 3)
    

    Gives:

    [[1]]
    [1] 1 2 3 4 5 6 7
    
    [[2]]
    [1]  8  9 10 11 12 13
    
    [[3]]
    [1] 14 15 16 17 18 19 20
    

提交回复
热议问题