I want to convert my string ID column into numeric ID column.
For this objective, I'm using the following 2 hash functions: digest() and digest2int().
digest() from library(digest) requires R object as input.
When calling digest() within the dplyr::mutate,instead of applying the digest() for each value from column Species, it only gives 1 unique value across all Species values.
chk <- as_tibble(iris) %>% select(Species) %>% unique() %>%
mutate(digest_Species = digest(Species, algo = 'md5')) %>%
mutate(digest2int_Species = digest2int(digest_Species))
chk
# A tibble: 3 x 3
Species digest_Species digest2int_Species
<fct> <chr> <int>
1 setosa 7d4660364a758d3a706737e96be30a54 1339653983
2 versicolor 7d4660364a758d3a706737e96be30a54 1339653983
3 virginica 7d4660364a758d3a706737e96be30a54 1339653983
The expectation is that for each Species value, a unique digest() output, and there should be unique digest2int() value for each Species value.
# When applied individually, we get unique values for each Species color
> 'setosa' |> digest(algo = 'md5') |> digest2int()
[1] -1179682887
> 'versicolor' |> digest(algo = 'md5') |> digest2int()
[1] 1134371908
> 'virginica' |> digest(algo = 'md5') |> digest2int()
[1] 1387061319
I still require the digest() because I think, it will further avoid possible collisions.
There're some cases from my dataset where digest2int() gave out similar output for completely 2 different strings.