I have a dataframe which I want to assign IDs to each location and site. To do this I want to create a new column of ‘ID’ which I’ll be a number of 1 to however many based on the number of sites in each location code. I have included how I would like the outcome bellow. My main problem in doing this is dealing with some duplicate values (I would prefer to keep these in as it solved a wider issue away from here), when I use a seq_along to give the sites an ID the duplicates ones obviously dont get the same ID. To do this I was grouping by locationcode and calling seq_along over my site column. I think my theory is ok here I might just need a different function that can identify the duplicates and give them the same IDs.
locationcode site
167 Central
167 Suburbs
167 Rural
167 Rural 2
167 Rural 3
167 Rural 3
167 Rural 4
17 Rural 4
17 Central
17 Suburbs
17 Rural
17 Rural
17 Rural 3
17 Rural 3
17 Rural 5
17 Rural 4
11 Central
11 Suburbs
11 Rural
166 Central
166 Central
What I am trying to achieve;
locationcode site ID
167 Central 1
167 Suburbs 2
167 Rural 3
167 Rural 2 4
167 Rural 3 4
167 Rural 3 4
167 Rural 4 5
17 Rural 4 5
17 Central 1
17 Suburbs 2
17 Rural 3
17 Rural 3
17 Rural 3 4
17 Rural 3 4
17 Rural 5 5
17 Rural 7 5
11 Central 1
11 Suburbs 2
11 Rural 3
166 Central 1
166 Central 1