I have a very long data frame (over 300 000 rows), consisting of all subjects trials of a task, in long format. So about 300 rows is successive trials for one subject, followed by the other subjects underneath. What I want to do is create a new column with trial numbers for every subject.
Such as:
subject trial_number
101 1
101 2
101 3
102 1
102 2
102 3
I am thinking I should somehow make R choose a subject number from column subject, and then create an ascending list and then loop this over all the subject numbers. But I have not been able to figure out how to loop over subject numbers, while also creating an ascending list in one and the same column over these subjects? The different was of creating new columns I have seen are all based on calculations or values in other columns, where for me the values in the new column is not based on a calculation of or value of another column.
I also thought of splitting the data frame into smaller ones based on subject number, create ascending lists and the merge them again? Seems like a very inefficient way to do it though?
I don't have example code for failed attempts or so, as I haven't been able to figure out how to structure this. I'm thinking some kind of combination of subset and within? Or are there better solutions my googling skills haven't helped me find yet?