
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Unsplit a Split Data Table Object in R
To unsplit a split data.table object in R, we can follow the below steps −
First of all, create a data.table object.
Then, use split function to split the data.table object.
After that, use do.call function along with rbind function unsplit the data frame.
Example
Create the data.table object
Let’s create a data.table object as shown below −
library(data.table) Grp<-sample(LETTERS[1:4],25,replace=TRUE) Score<-sample(1:50,25) DT<-data.table(Grp,Score) DT
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
Grp Score 1: D 13 2: D 25 3: A 21 4: A 1 5: C 4 6: B 16 7: A 28 8: A 6 9: C 26 10: D 2 11: A 47 12: D 38 13: A 49 14: B 31 15: A 36 16: C 41 17: D 24 18: C 27 19: C 34 20: D 7 21: D 42 22: C 14 23: B 44 24: B 30 25: D 19 Grp Score
Split the data.table object
Using split function to split the data.table object −
library(data.table) Grp<-sample(LETTERS[1:4],25,replace=TRUE) Score<-sample(1:50,25) DT<-data.table(Grp,Score) new_DT<-split(DT,DT$Grp) new_DT
Output
$A Grp Score 1: A 21 2: A 1 3: A 28 4: A 6 5: A 47 6: A 49 7: A 36 $B Grp Score 1: B 16 2: B 31 3: B 44 4: B 30 $C Grp Score 1: C 4 2: C 26 3: C 41 4: C 27 5: C 34 6: C 14 $D Grp Score 1: D 13 2: D 25 3: D 2 4: D 38 5: D 24 6: D 7 7: D 42 8: D 19
Unsplit the data.table object
Using do.call function along with rbind function unsplit the data.table object DT −
library(data.table) Grp<-sample(LETTERS[1:4],25,replace=TRUE) Score<-sample(1:50,25) DT<-data.table(Grp,Score) new_DT<-split(DT,DT$Grp) do.call("rbind",new_DT)
Output
Grp Score 1: A 21 2: A 1 3: A 28 4: A 6 5: A 47 6: A 49 7: A 36 8: B 16 9: B 31 10: B 44 11: B 30 12: C 4 13: C 26 14: C 41 15: C 27 16: C 34 17: C 14 18: D 13 19: D 25 20: D 2 21: D 38 22: D 24 23: D 7 24: D 42 25: D 19 Grp Score
Advertisements