1

I have data like this

a1  a2   a3
1   22   44
1   22   33
2   44    3
2   55   5
3   22   7

I like to give unique ids based on the a1 and a2 combination:

a1  a2   a3   id
1   22   44    1
1   22   33    1
2   44    3    2
2   55   5     3
3   22   7     4

Thanks.

user1582755
  • 213
  • 1
  • 2
  • 9

1 Answers1

1

The most straightforward way in base R is to probably use factor:

> as.integer(factor(with(mydf, paste(a1, a2))))
[1] 1 1 2 3 4
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485