Thanks, @jrmuirhead, for this.
This works on this simple case. But in my actual data the numbering continues. For example, subject1 has about 8 different values for start_time the number works fine from 1 o 8, but with the next subject, the numbering continues from 9 to n. It does not restart from 1 to n. Note that each subject has their own start_time which does not always correspond to other subject.
I want it to restart from 1 to n for each start_time value within subject and word. Sorry if the example I provided earlier did not represent the data correctly.
Here is a more representative data building on yours:
d <- tibble::tribble(
~subject, ~start_time, ~word, ~label,
"s1", 1.00, "go" , "g",
"s1", 1.00, "go", "g",
"s1", 1.20, "eat", "t",
"s1", 1.20, "eat" , "t",
"s2", 2.00, "go" , "g",
"s2", 2.00, "go" , "g",
"s2", 2.20, "eat" , "t",
"s2", 2.20, "eat" , "t",
"s3", 3.00, "go" , "g",
"s3", 3.00, "go" , "g",
"s3", 3.20, "eat" , "t",
"s3", 3.20, "eat" , "t"
)
when I follow the approach you kindly suggested, it gives the following output:
d <- d %>%
rowid_to_column(., var = "rowid") %>%
group_by(start_time) %>%
mutate(id = cur_group_id()) %>%
ungroup() %>%
arrange(rowid)
d
# A tibble: 12 x 6
rowid subject start_time word label id
<int> <chr> <dbl> <chr> <chr> <int>
1 1 s1 1 go g 1
2 2 s1 1 go g 1
3 3 s1 1.2 eat t 2
4 4 s1 1.2 eat t 2
5 5 s2 2 go g 3
6 6 s2 2 go g 3
7 7 s2 2.2 eat t 4
8 8 s2 2.2 eat t 4
9 9 s3 3 go g 5
10 10 s3 3 go g 5
11 11 s3 3.2 eat t 6
12 12 s3 3.2 eat t 6
Note how the numbering continues. Here is the output that I want to get.
# A tibble: 12 x 6
rowid subject start_time word label id
<int> <chr> <dbl> <chr> <chr> <int>
1 1 s1 1 go g 1
2 2 s1 1 go g 1
3 3 s1 1.2 eat t 2
4 4 s1 1.2 eat t 2
5 5 s2 2 go g 1
6 6 s2 2 go g 1
7 7 s2 2.2 eat t 2
8 8 s2 2.2 eat t 2
9 9 s3 3 go g 1
10 10 s3 3 go g 1
11 11 s3 3.2 eat t 2
12 12 s3 3.2 eat t 2
I apologize again for not making this clear earlier.