I have a dataframe that currently looks like this:
subjectID | Trial |
---|---|
1 | 3 |
1 | 3 |
1 | 3 |
1 | 4 |
1 | 4 |
1 | 5 |
1 | 5 |
1 | 5 |
2 | 1 |
2 | 1 |
2 | 3 |
2 | 3 |
2 | 3 |
2 | 5 |
2 | 5 |
2 | 6 |
3 | 1 |
Etc., where trial number is nested under subject ID. I need to make a new column in which column "NewTrial" is simply what order the trials now appear in. For example:
subjectID | Trial | NewTrial |
---|---|---|
1 | 3 | 1 |
1 | 3 | 1 |
1 | 3 | 1 |
1 | 4 | 2 |
1 | 4 | 2 |
1 | 5 | 3 |
1 | 5 | 3 |
1 | 5 | 3 |
2 | 1 | 1 |
2 | 1 | 1 |
2 | 3 | 2 |
2 | 3 | 2 |
2 | 3 | 2 |
2 | 5 | 3 |
2 | 5 | 3 |
2 | 6 | 4 |
3 | 1 | 1 |
So far, I have a for-loop written that looks like this:
for (myperson in unique(data$subjectID)){
#This line creates a vector of the number of unique trials per subject: for subject 1, c(1, 2, 3)
triallength=1:length(unique(data$Trial[data$subID==myperson]))
I'm having trouble now finding a way to paste the numbers from the created triallength
vector as a column in the dataframe. Does anyone know of a way to accomplish this? I am lacking some experience with for-loops and hoping to gain more. If anyone has a tidyverse/dplyr solution, however, I am open to that as well as an alternative to a for-loop. Thanks in advance, and let me know if any clarification is needed!