Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Encode unique observations using identifier

I have a data frame where one column is consisting of strings, which is a unique identifier to a journey. A reproducible data frame:

df <- data.frame(tours = c("ansc123123", "ansc123123", "ansc123123", "baa3999", "baa3999", "baa3999"),
                 order = rep(c(1, 2, 3), 2))

Now my real data is much larger with many more observations and unique identifiers, but I would like to have an output on the format as when you do something like this (but not manually encoded), so that the journeys with the same tours value get encoded as the same journey.

df$journey <- c(1, 1, 1, 2, 2, 2)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can convert it to a factor.

df$journey <- as.integer(factor(df$tours))

df$journey
#[1] 1 1 1 2 2 2

Or use match and unique.

match(df$tours, unique(df$tours))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading