How to create an ID column for the combination of values in multiple columns in R data frame?

R ProgrammingServer Side ProgrammingProgramming

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 −

 Live Demo

> 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
raja
Published on 03-Aug-2021 10:59:19
Advertisements