I have a state column in a dataframe and I want to create two new columns:
One that looks ahead to the next stage change and one that looks back to the previous state change. So the resulting dataframe will look like below:
state coming previous
a a-b NA
a a-b NA
a a-b NA
a a-b NA
b b-c a-b
b b-c a-b
b b-c a-b
c c-a b-c
c c-a b-c
c c-a b-c
a NA c-a
a NA c-a
Or maybe even better, but now you just create two transition columns:
state trans1 trans2
a a-b NA
a a-b NA
a a-b NA
a a-b NA
b a-b b-c
b a-b b-c
b a-b b-c
c c-a b-c
c c-a b-c
c c-a b-c
a c-a NA
a c-a NA
[Edit]
changed states named “1” to “c” because it was confusing
Let’s give that dataframe a name, say ‘inp’. Use the
rlefunction to construct the sequence of “states”:(I was able to overcome my difficulty with understanding your first request, but had persistent difficulty with the second part.)