How to assign a counter to a specific subset of a data.frame which is defined by a factor combination?
问题 My question is: I have a data frame with some factor variables. I now want to assign a new vector to this data frame, which creates an index for each subset of those factor variables. data <-data.frame(fac1=factor(rep(1:2,5)), fac2=sample(letters[1:3],10,rep=T)) Gives me something like: fac1 fac2 1 1 a 2 2 c 3 1 b 4 2 a 5 1 c 6 2 b 7 1 a 8 2 a 9 1 b 10 2 c And what I want is a combination counter which counts the occurrence of each factor combination. Like this fac1 fac2 counter 1 1 a 1 2 2 c