split & unsplit Functions in R (2 Examples)
In this R tutorial you’ll learn how to divide (and reassemble) vectors into groups using the split function.
The tutorial is structured as follows:
Let’s dig in:
Definitions & Basic R Syntaxes of split and unsplit Functions
Definitions: Please find the definitions of the split and unsplit functions below.
- The split R function divides data into groups.
- The unsplit R function reverses the output of the split function.
Basic R Syntaxes: You can find the basic R programming syntaxes of the split and unsplit functions below.
split(values, groups) # Basic R syntax of split function unsplit(split_values, groups) # Basic R syntax of unsplit function
I’ll illustrate in the following two examples how to apply the split and unsplit functions in the R programming language.
Creation of Example Data
First, we’ll need to create some data that we can use in the example code below:
vec <- 1:10 # Create example vector vec # Print example vector # 1 2 3 4 5 6 7 8 9 10
Have a look at the previous output of the RStudio console. It shows that our example data is a simple numeric vector ranging from 1 to 10.
For the application of the split and unsplit commands, we also have to create a grouping vector:
groups <- c(rep("A", 3), # Create grouping vector rep("B", 5), rep("C", 2)) groups # Print grouping vector # "A" "A" "A" "B" "B" "B" "B" "B" "C" "C"
Our grouping vector contains of ten elements that are separating our data into three groups A, B, and C.
Example 1: Using split() Function in R
This Example explains how to divide our example data using the split function in R. Have a look at the following programming syntax and its output:
my_split <- split(vec, groups) # Apply split function my_split # Print output # $A # [1] 1 2 3 # # $B # [1] 4 5 6 7 8 # # $C # [1] 9 10
As you can see, the previous R code created a list called my_split, which contains three list elements. Each list elements consists of the values that correspond to the groups A, B, and C.
Example 2: Using unsplit() Function in R
It is also possible to reverse the output of the split function using unsplit. In Example 2, I’ll show how to do that:
my_unsplit <- unsplit(my_split, groups) # Apply unsplit function my_unsplit # Print output # 1 2 3 4 5 6 7 8 9 10
As you can see, the previous output is exactly the same as our original vector. In other words: the output of the split function was reversed.
Video & Further Resources
In case you need further information on the R code of this article, I can recommend to watch the following video which I have published on my YouTube channel. In the video, I’m explaining the R codes of this article:
In addition to the video, you might read the related articles of my website:
- Split Data Frame in R
- Split Data Frame into List of Data Frames Based On ID Column
- Split Data Frame Variable into Multiple Columns
- str_split & str_split_fixed Functions
- R Functions List (+ Examples)
- The R Programming Language
This page showed how to apply the split and unsplit functions in the R programming language. Tell me about it in the comments section, if you have any further questions.