Split a vector into chunks

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

    Sorry if this answer comes so late, but maybe it can be useful for someone else. Actually there is a very useful solution to this problem, explained at the end of ?split.

    > testVector <- c(1:10) #I want to divide it into 5 parts
    > VectorList <- split(testVector, 1:5)
    > VectorList
    $`1`
    [1] 1 6
    
    $`2`
    [1] 2 7
    
    $`3`
    [1] 3 8
    
    $`4`
    [1] 4 9
    
    $`5`
    [1]  5 10
    

提交回复
热议问题