I have a data.frame with two columns (FirstName and State).
my.df = data.frame(FirstName = c('John', 'Paul', 'John', 'Sarah', 'Haley', 'Paul', 'John'),
State = c('VIC', 'NSW', 'VIC', 'QLD', 'TAS', 'NSW', 'VIC'))
FirstName State
John VIC
Paul NSW
John VIC
Sarah QLD
Haley TAS
Paul NSW
John VIC
I would like to include an additional column that lists the nth occurance for each value in the FirstName column. For example, 'John' appears in rows 1, 3 and 6 - the new column would therefore list '1' in row 1, '2' in row 3 (as this is the second time 'John' is listed) and '3' in row 6 (as this is the third time 'John' is listed).
My desired outcome would appear as follows:
FirstName State Index
John VIC 1
Paul NSW 1
John VIC 2
Sarah QLD 1
Haley TAS 1
Paul NSW 2
John VIC 3
Any assistance would be appreciated