0

I have a quite large data frame containing among others two columns: towns and counties, and a bunch of lines corresponding to combinations of the two. I would like to get a new column with a unique ID for every combinations. I could probably do it in base R, but I am trying to convert to dplyr for this project.

The following code gives me an ID for each combination of two variables in the data frame.

library(dplyr)

dd <- data.frame(x = sample(letters[1:10], 100, TRUE), y = sample(letters[1:10], 100, TRUE))


id <- dd %>% distinct(x, y) %>% mutate(1:n())

How can add this id variable back to dd in the correct positions?

Theodor
  • 986
  • 3
  • 7
  • 23

0 Answers0