- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create an ID column for the combination of values in multiple columns in R data frame?
To create an ID column for the combination of values in multiple columns in R data frame, we can follow the below steps −
- First of all, create a data frame.
- Then, create an ID column using as.numeric, as.factor and with function for the combination of values in columns of the data frame.
Create the data frame
Let's create a data frame as shown below −
> x1<-rpois(20,1) > x2<-rpois(20,1) > df<-data.frame(x1,x2) > df
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
x1 x2 1 0 1 2 0 1 3 0 1 4 2 2 5 4 0 6 1 0 7 3 2 8 2 0 9 0 0 10 1 0 11 0 1 12 3 1 13 2 0 14 0 1 15 0 0 16 0 1 17 1 0 18 1 4 19 0 3 20 1 0
Create an ID column for combinations
Using as.numeric function along with as.factor and with function to create an ID column for the combination of values in columns x1 and x2 −
> df$Pairs_ID<-as.numeric(as.factor(with(df,paste(x1,x2,sep="_")))) > df
Output
x1 x2 Pairs_ID 1 0 1 2 2 0 1 2 3 0 1 2 4 2 2 7 5 4 0 10 6 1 0 4 7 3 2 9 8 2 0 6 9 0 0 1 10 1 0 4 11 0 1 2 12 3 1 8 13 2 0 6 14 0 1 2 15 0 0 1 16 0 1 2 17 1 0 4 18 1 4 5 19 0 3 3 20 1 0 4
- Related Articles
- How to convert multiple columns into single column in an R data frame?
- Find the percentiles for multiple columns in an R data frame.
- How to create group names for consecutively duplicate values in an R data frame column?
- How to create a new column with means of row values for each or some of the columns in an R data frame?
- How to create a table of frequency for range of values in an R data frame column?
- How to create a subset of an R data frame based on multiple columns?
- How to concatenate column values and create a new column in an R data frame?
- How to create histogram for discrete column in an R data frame?
- How to find the sum of numerical columns based on the combination of values in\ncategorical columns in R data frame?
- How to create a column in an R data frame that contains the multiplication of two columns?
- How to replace NA values in columns of an R data frame form the mean of that column?
- How to create histogram of all columns in an R data frame?
- How to change a data frame with comma separated values in columns to multiple columns in R?
- How to create a column with the serial number of values in character column of an R data frame?
- How to create barplot for some top values in an R data frame?

Advertisements