I have following dataframe in R
ID Season Year Weekday 1 Winter 2017 Monday 2 Winter 2018 Tuesday 3
We can use match with unique elements
match
unique
library(dplyr) dat %>% mutate_all(funs(match(., unique(.)))) # ID Season Year Weekday #1 1 1 1 1 #2 2 1 2 2 #3 3 2 1 1 #4 4 2 2 3