Problem:
I want to reset a (1,2)
sequence if condition is met (subject changes).
I have for
and if
loops that will do this but, unsurprisingly, that method is very slow.
Any suggestions (e.g., involving the apply family) for a more efficent method?
Current:
subj odd_even
a
a
a
b
b
b
b
c
c
c
Goal:
subj odd_even
a 1
a 2
a 1
b 1
b 2
b 1
b 2
c 1
c 2
c 1
df = data.frame( subj = c("a","a","a","b","b","b","b", "c","c","c"), odd_even = "" )
I like the
sequence
function for this:data.table is another option:
Benchmarks: