2

I need to create a new column with numeric ID's which is corresponding to a column with string ID. I want it to look like this:

Numeric ID    String ID
1           2RRRRkbihdUTz/X
1           2RRRRkbihdUTz/X
1           2RRRRkbihdUTz/X
2           A09mqMKPnBgUf
2           A09mqMKPnBgUf
2           A09mqMKPnBgUf
2           A09mqMKPnBgUf
3           AaULuZXJWriMXM
3           AaULuZXJWriMXM
3           AaULuZXJWriMXM  

What function can I use to generate the Numeric ID, which is the same for rows with the same string ID?

1 Answers1

1

We can use match to create the 'NumericID'

with(dfn, match(StringID, unique(StringID)))

Or another option is to convert to factor with levels specified as the unique values and then coerce it to integer

with(dfn, as.integer(factor(StringID, levels = unique(StringID))))
akrun
  • 874,273
  • 37
  • 540
  • 662