I have the following dataset
varA <- c(rep("A",2), rep("B",4))
varB <- c(rep("aaaa",2), rep("bbbb", 3), rep("cccc",1) )
dat <- data.frame(varA, varB)
dat
varA varB
1 A aaaa
2 A aaaa
3 B bbbb
4 B bbbb
5 B bbbb
6 B cccc
I would like to generate ids for each subgroup, such that the first subgroup is 1, the second 2, etc, within varA. Theids can repeat across the dataset, just not within subgroup.
This the needed result
varA varB res
1 A aaaa 1
2 A aaaa 1
3 B bbbb 1
4 B bbbb 1
5 B bbbb 1
6 B cccc 2
How can I do this with R ?
I tried cur_group_id() in dplyr but it is not working for me...
thanks!!