0

I have 14000 odd obs.

----------
shopper_id
----------
4590FBB2C98F98BF51769EB162F527D9

135BC3FBD2C32B6F2DC6BAA92EA63747

DCF1C2CD4351DF74551E7AE3A5365983

83C4D97CA2DEF71ED33DF8A8D6F8E864

AD20D56153044D0F729EFC10A3DF0F71

ShopperId is a unique ID in my dataset, I want to run apriori.

My code:

df_sorted <- beauty[order(beauty$shopper_id),]
df_sorted$shopper_id <-as.numeric(df_sorted$shopper_id)

I am getting the below error:

Warning message:
NAs introduced by coercion
Sotos
  • 51,121
  • 6
  • 32
  • 66
  • shopper_id isn't numeric, you can't do as.numeric on it. – Terru_theTerror Feb 09 '18 at 09:10
  • 3
    `as.numeric(as.factor(df_sorted$shopper_id))` – user20650 Feb 09 '18 at 09:10
  • 1
    How do you expect to convert a string to number? What are you expecting to get? The numbers within the string or assign a value to each string? – Sotos Feb 09 '18 at 09:10
  • Your `shopper_id` is not numeric, to be certain, nor does it seem to have any non random structure. You should be able to just order by it directly to group shoppers together. – Tim Biegeleisen Feb 09 '18 at 09:11
  • relevant: https://stackoverflow.com/questions/9289258/how-to-create-id-column-in-r , https://stackoverflow.com/questions/35558799/r-transform-a-factor-id-variable-into-a-numeric-id-variable – user20650 Feb 09 '18 at 09:11
  • is it hexadecimal value? then use this link https://stackoverflow.com/questions/9820165/convert-hexadecimal-string-to-its-numerical-values-in-c-sharp – Ranjith A Francis Feb 09 '18 at 09:17

1 Answers1

0

Use dplyr::dense_rank. It gives each distinct shopper_id a number based on its sorted order.

shoper_id <- c("A", "A", "B", "A", "B", "B", "C", "C", "C")

dplyr::dense_rank(shoper_id)
#> [1] 1 1 2 1 2 2 3 3 3
Paul
  • 8,734
  • 1
  • 26
  • 36