I am looking for a way to generate a sequence for a column with names of cities grouped by an ID. What is crucial is that when a name of a city is repeated (within the group) a new sequence has to start. A new sequence should also start in case of a new ID.
EDIT:
The question how to create the above mentioned sequence has been solved. To help select the row with the highest sequence number later on, I am looking for a way to add a new column to the data frame that shows for each record, per sequence, per ID the highest number of each sequence.
Here is an example of what I want to achieve, based on a simplified version of my data frame:
ID City Sequence Highest_number
1 Nijmegen 1 2
1 Nijmegen 2 2
1 Arnhem 1 2
1 Arnhem 2 2
1 Nijmegen 1 1
1 Arnhem 1 3
1 Arnhem 2 3
1 Arnhem 3 3
1 Nijmegen 1 1
2 Nijmegen 1 1
2 Utrecht 1 1
2 Amsterdam 1 2
2 Amsterdam 2 2
2 Utrecht 1 4
2 Utrecht 2 4
2 Utrecht 3 4
2 Utrecht 4 4
mydf <- data.frame(ID = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2),
City = c("Nijmegen", "Nijmegen", "Arnhem", "Arnhem", "Nijmegen",
"Arnhem", "Arnhem","Arnhem", "Nijmegen", "Nijmegen", "Utrecht",
"Amsterdam", "Amsterdam", "Utrecht", "Utrecht", "Utrecht", "Utrecht"))