Anuj Sharma Anuj Sharma - 11 days ago 5
R Question

Replace similar columns with some numerical values

I have dataframe like this:

Hashed_User_Id

[1] f2de2b4a6011a1ab52d3aefbc9b8a4103d7574f4
[2] 88cb5d85c41abb7ad99595ceb7c2fc98409dd4dc
[3] 25313021517412ce58072d798ccea29ba5d2f427
[4] f2de2b4a6011a1ab52d3aefbc9b8a4103d7574f4
[5] 88cb5d85c41abb7ad99595ceb7c2fc98409dd4dc
[6] 25313021517412ce58072d798ccea29ba5d2f427


I want to replace these hashed values by numeric values keeping same number for same values, something like this:

Hashed_User_Id

[1] 1
[2] 2
[3] 3
[4] 1
[5] 2
[6] 3


How can I achieve this?

Answer

As Ronak suggested,

as.integer(as.factor(Hashed_User_Id))