
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
Create a Group Column in an R Data Frame
Suppose we have a data frame called df that contains two columns say X and Y then we can create a group column based on X and Y by converting df into a data.table object and creating list of values in X and Y with list function.
Check out the below Examples to understand how it can be done.
Example 1
Following snippet creates a sample data frame −
x1<-rpois(20,2) x2<-rpois(20,2) df1<-data.frame(x1,x2) df1
The following dataframe is created
x1 x2 1 1 5 2 1 1 3 0 1 4 1 1 5 2 1 6 2 3 7 4 2 8 0 4 9 2 1 10 2 2 11 0 1 12 3 1 13 3 3 14 4 3 15 2 0 16 1 3 17 2 1 18 3 1 19 5 1 20 3 2
To load data.table object and create a group column for values in df1 on the above created data frame, add the following code to the above snippet −
x1<-rpois(20,2) x2<-rpois(20,2) df1<-data.frame(x1,x2) library(data.table) setDT(df1)[,Group:=.GRP,by=list(x1,x2)] df1
Output
If you execute all the above given snippets as a single program, it generates the following Output −
x1 x2 Group 1: 1 5 1 2: 1 1 2 3: 0 1 3 4: 1 1 2 5: 2 1 4 6: 2 3 5 7: 4 2 6 8: 0 4 7 9: 2 1 4 10: 2 2 8 11: 0 1 3 12: 3 1 9 13: 3 3 10 14: 4 3 11 15: 2 0 12 16: 1 3 13 17: 2 1 4 18: 3 1 9 19: 5 1 14 20: 3 2 15
Example 2
Following snippet creates a sample data frame −
y1<-sample(LETTERS[1:4],20,replace=TRUE) y2<-sample(LETTERS[1:4],20,replace=TRUE) df2<-data.frame(y1,y2) df2
The following dataframe is created
y1 y2 1 B C 2 B D 3 D C 4 D B 5 A B 6 D A 7 A C 8 A C 9 D B 10 C C 11 A A 12 C D 13 B C 14 C C 15 A A 16 D B 17 B A 18 C B 19 C B 20 C B
To create a group column for values in df2 on the above created data frame, add the following code to the above snippet −
y1<-sample(LETTERS[1:4],20,replace=TRUE) y2<-sample(LETTERS[1:4],20,replace=TRUE) df2<-data.frame(y1,y2) setDT(df2)[,Group:=.GRP,by=list(y1,y2)] df2
Output
If you execute all the above given snippets as a single program, it generates the following Output −
y1 y2 Group 1: B C 1 2: B D 2 3: D C 3 4: D B 4 5: A B 5 6: D A 6 7: A C 7 8: A C 7 9: D B 4 10: C C 8 11: A A 9 12: C D 10 13: B C 1 14: C C 8 15: A A 9 16: D B 4 17: B A 11 18: C B 12 19: C B 12 20: C B 12