1

I have a dataset with character ID which I want to convert to numeric one. I know I can type out each character ID and recode them, but because I don't care about using specific numbers is there a faster way to just randomly pick an unique number for each character ID? Example below:

 ID
1_1_1_aa
1_1_2_aa
1_1_3_aa
1_2_1_aa
1_2_2_aa
1_2_3_aa
1_1_1_bb
1_1_2_bb
1_1_3_bb
1_2_1_bb
1_2_2_bb
1_2_3_bb

Here is what I want"

ID
1
2
3
4
5
6
7
8
9
10
11
12

I have a lot more character IDs and so I don't want to type them all out and use recode. How to do this more efficiently? Any help is greatly appreciated!

cliu
  • 667
  • 4
  • 11

1 Answers1

2

We can use

df$ID <- match(df1$ID, unique(df1$ID))
akrun
  • 789,025
  • 32
  • 460
  • 575