Split Vector into Chunks in R (2 Examples)
On this page, I’ll show how to divide a vector or array into groups in R.
Table of contents:
Let’s dig in:
Creating Example Data
The data below is used as basement for this R programming language tutorial:
my_vec <- 1:100 # Create example vector my_vec # Print first values of example vector # [1] 1 2 3 4 5 6 7 8 9 10 11 12 ...
Have a look at the previous output of the RStudio console. It shows that our example data is a vector consisting of 100 numeric values that are ranging from 1 to 100.
Example 1: Split Vector into Chunks Given the Length of Each Chunk
In Example 1, I’ll show how to divide a vector into subgroups when we know the number of elements each group should contain.
First, we have to specify the number of elements in each group (i.e. 20):
chunk_length <- 20 # Define number of elements in chunks
Now, we can use the split, ceiling, and seq_along functions to divide our vector into chunks:
split(my_vec, # Applying split() function ceiling(seq_along(my_vec) / chunk_length)) # $`1` # [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 # # $`2` # [1] 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 # # $`3` # [1] 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 # # $`4` # [1] 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 # # $`5` # [1] 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
The output is a list consisting of five list elements. Each list element contains 20 values of our input vector.
Example 2: Split Vector into Chunks Given the Number of Chunks
This example illustrates how to divide a vector into subgroups when we know the number of subgroups that we want to create.
First, we have to set a number of subgroups we want to create (i.e. seven):
chunk_number <- 7 # Define number of chunks
Now, we can apply the split, cut, and seq_along functions to create our seven chunks:
split(my_vec, # Applying split() function cut(seq_along(my_vec), chunk_number, labels = FALSE)) # $`1` # [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # # $`2` # [1] 16 17 18 19 20 21 22 23 24 25 26 27 28 29 # # $`3` # [1] 30 31 32 33 34 35 36 37 38 39 40 41 42 43 # # $`4` # [1] 44 45 46 47 48 49 50 51 52 53 54 55 56 57 # # $`5` # [1] 58 59 60 61 62 63 64 65 66 67 68 69 70 71 # # $`6` # [1] 72 73 74 75 76 77 78 79 80 81 82 83 84 85 # # $`7` # [1] 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
The output is a list with seven list elements.
Video & Further Resources
I have recently released a video on my YouTube channel, which illustrates the R programming syntax of this tutorial. You can find the video below:
In addition, you might have a look at some of the related tutorials of www.statisticsglobe.com:
- Split Data Frame into List of Data Frames Based On ID Column
- Split Data Frame Variable into Multiple Columns
- Split Data into Train & Test Sets
- R Programming Examples
You learned in this tutorial how to split vectors into different parts in R. Let me know in the comments section, if you have any further questions.