Say if we have a dataframe looking like this:
A B C D
XX WW 0 0
XX WW 0 1
WW XX 0 1
XX WW 1 1
XX WW 1 0
YY ZZ 0 1
YY ZZ 0 1
ZZ YY 1 0
YY ZZ 1 1
I would like to add a new row down to each one in which D equals 1 and A and B remain unchanged into the following one. The rule that should follow the entire table is that whenever D equals one, A and B should invert its values into the following one, assuming C and D will equal 0 into these newly introduced rows. In this example, my desired output should look this way:
A B C D
XX WW 0 0
XX WW 0 1
WW XX 0 1
XX WW 1 1
WW XX 0 0
XX WW 1 0
YY ZZ 0 1
ZZ YY 0 0
YY ZZ 0 1
ZZ YY 1 0
YY ZZ 1 1
I've been unsuccessfully trying with dplyr and tidyr, any piece of help will be more than appreciated.