In R, I have a data frame with a time column that I want to revert to one or zero every time the text in another column changes. The text never goes back to the previous text.
There are 395 times the text changes and ~270’000 rows.
Table at the moment
| Time | Trial |
|---|---|
| 11 | A |
| 12 | A |
| 13 | B |
| 14 | B |
Table wanted
| Time | Trial |
|---|---|
| 1 | A |
| 2 | A |
| 1 | B |
| 2 | B |
>Solution :
We could group and then use row_number()
library(dplyr)
df %>%
group_by(Trial) %>%
mutate(Time = row_number()) %>%
ungroup()
Time Trial
<int> <chr>
1 1 A
2 2 A
3 1 B
4 2 B