Split & Unsplit Functions In R (2 Examples) - Statistics Globe
Maybe your like
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:
1) Definitions & Basic R Syntaxes of split and unsplit Functions 2) Creation of Example Data 3) Example 1: Using split() Function in R 4) Example 2: Using unsplit() Function in R 5) Video & Further Resources 6) Subscribe to the Statistics Globe Newsletter 7) Thank you!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 functionI’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 10Have 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 10As 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 10As 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.
Get regular updates on the latest tutorials, offers & news at Statistics Globe.I hate spam & you may opt out anytime: Privacy Policy.
SubscribeLoading...
Thank you!
Please check your email inbox and click the confirmation link to complete your subscription. If you don’t see the email within a few minutes, please also check your spam/junk folder.
Tag » How To Split In R
-
How To Split Vector And Data Frame In R - R-bloggers
-
SPLIT In R With Split() Function [Learn How To Split Data With Examples]
-
Split: Divide Into Groups And Reassemble - RDocumentation
-
Split In R: How To Split Vector And Data Frame In R - R-Lang
-
How To Use Split() Function In R To Split Data - Statology
-
Divide The Data Into Groups In R Programming - Split() Function
-
How To Split Data Into Training/testing Sets Using Sample Function
-
Split Up A String Into Pieces. — Str_split • Stringr
-
How To Use Strsplit() Function In R? - DigitalOcean
-
Split Data Frame In R (3 Examples) | Divide (Randomly) By Row ...
-
Split - MAKE ME ANALYST
-
R: Split The Strings In A Vector - UCLA Math
-
Split The Elements Of A Character Vector - R
-
How To Split Text Strings In Displayr Using R