So this is some sample data I have:
signal1 signal2 signal3
1 0 1
1 1 1
1 0 1
1 0 1
1 0 1
1 0 1
1 0 0
And I want to add a fourth column, such that whenever signal1==signal2==signal3
are both 1, the new column repeats 1s until signal3 changes from 1 to 0.
So from the above example, I want to generate something like:
signal1 signal2 signal3 signal_generate
1 0 1 0
1 1 1 1
1 0 1 1
1 0 1 1
1 0 1 1
1 0 1 1
1 0 0 0
I'm thinking this would be achieved by an ifelse
statement, but I'm having a lot of difficulty over this seemingly simple task. I'm thinking of doing something along the lines of:
signal_generate <- ifelse(data[,1]==data[,2]&data[,3]>0, rep(data[,3]==1), 0)
But I can't figure out how to get signal3
to repeat the correct number of times.
Edit: I'm realizing an ifelse
would be silly because it would only look at rows in which the condition signal1 == signal2 == signal3
meet exactly. I'm still terribly stuck, and help would be much appreciated!