You can use dplyr::group_indices()
:
NSE version
group_indices(df, x, y)
# [1] 1 1 2 3 4 4
SE version
group_indices_(df, .dots = names(df))
# [1] 1 1 2 3 4 4
The unfortunate side of this function is that it doesn't work with mutate
function (yet), so you have to use it as:
df$pattern <- group_indices(df, x, y)
From the linked answer, it seems that even though the non-standard evaluation version doesn't work with mutate
, the standard evaluation version does:
df %>% mutate(pattern = group_indices_(df, .dots = c('x', 'y')))