Hi,
You can do this by creating factors and then converting them into integers like so
library(dplyr)
#Data
df = data.frame(
a = c(1,1,1,1,1,1,1,2,1,1),
b = c(1,1,1,2,2,1,3,4,3,2),
c = c(1,1,1,2,2,1,3,4,3,2)
)
#Base R implementation
df$key = as.integer(as.factor(paste(df$a, df$b, df$c, sep = "")))
#Tidyverse implementation
df = df %>% mutate(
key = paste(a, b, c, sep = "") %>%
as.factor() %>% as.integer()
)
df
#> a b c key
#> 1 1 1 1 1
#> 2 1 1 1 1
#> 3 1 1 1 1
#> 4 1 2 2 2
#> 5 1 2 2 2
#> 6 1 1 1 1
#> 7 1 3 3 3
#> 8 2 4 4 4
#> 9 1 3 3 3
#> 10 1 2 2 2
Created on 2021-12-12 by the reprex package (v2.0.1)
You first paste the columns abc into a string, then make a factor of this string, then convert it into and integer.
Hope this helps,
PJ