Search code examples
runiquedplyr

Column with numbers for unique values


I have a tibble in R studio that looks like this And I need to add a column that gives to each name the same unique number. So it should look like this

I have already tried things like the below code:

people >%>
gorup_by(Name) >%>
mutate(NameID = seq(1, along.with = unique(Name))) >%>
ungroup()

This code gave the same number for all the column. So it gave me a number 1 for every NameID.

I Also tried the below code:

people >%>
gorup_by(Name) >%>
mutate(NameID = seq(1, length(unique(Name)))) >%>
ungroup()

And I came with the same result. I think that I might need to use an if function or ifelse, but I really don't know what should be the correct syntax.

sorry for the format, is my first time asking a question here and I need to learn to write things better


Solution

  • With dplyr 1.0.0, we can use cur_group_id

    library(dplyr)
    df1 %>%
       group_by(Name = factor(Name, levels = unique(Name))) %>%
       mutate(NameID = cur_group_id())
    

    Or without using group_by, it can be a match

    df1 %>%
       mutate(NameID = match(Name, unique(Name)))
    

    Or coerce the factor to integer

    df1 %>%
        mutate(NameID = as.integer(factor(Name, levels = unique(Name))))