2

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

Daniel James
  • 1,381
  • 1
  • 10
  • 28
Ajrhjnd
  • 330
  • 1
  • 9

1 Answers1

2

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))))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Many thanks family. I have a feeling that I am gonna learn a lot thanks to people like you. It is really good to have this space. Thanks again! – Ajrhjnd Sep 30 '20 at 23:01