add counter column by arranging two variables (dplyr)
问题 I've been looking for a while here and there but I couldn't find any solution for my situation. I have a data frame with IDs and VAR mixed within it. Here below I tried to reproduced a sample require(dplyr) seed(123) N <- 3 T <- 4 id <- rep(letters[1:N], each = T) var <- rep(sample(seq(1:100),T),N) row <- sample(seq(1:(N*T)),replace = F) dt <- data.frame(ID=id,VAR=var,ROW=row) %>% arrange(ROW) %>% select(-ROW) and I'd like to arrange by ID and VAR and add a counter per group in order to get