0

I have a variable called user_id in a string format, like: u:fwHX u:fwHX u:fwHX u:h_lx u:x_hi u:x_hi

They all start with a "u:" and then followed by a set of letters. I want to transform each unique user_id into a string in the following format:

user_id user_id u:fwHX U1 u:fwHX U1 u:fwHX U1 u:h_lx U2 u:x_hi U3 u:x_hi U3

This new user_id variable can only go from U1 until U123 (e.g U321, U312,U123.....). And i have to use the across and apply function to solve it. Thank you for the help!

Anja.L
  • 11
  • 2

1 Answers1

0

We can use match to convert the 'user_id' to index based on unique values and then paste the 'U' as prefix

df1$user_id_new <- with(df1, paste0("U", match(user_id, unique(user_id))))
akrun
  • 874,273
  • 37
  • 540
  • 662