Problem:
The starting states of "b" and "c" are determined by presence in another table.
dd$b[1] <- ifelse( "b" %in% table,1,0)  
dd$c[1] <- ifelse( "c" %in% table,1,0)  
I'm hoping to come up with a formula that:
- Checks to see if the value in column acontains "lamp" and "b" and makesba 0 if so,
- Else checks to see if the value in column acontains "lion" and "b" and makesba 1 if so,
- Or defaults to the value in the previous row for b.
To include to example outputs, the column c is included and should follow the same rules as the above.
Referring to the cell value above is crucial as a third step, because this is designed "change states" as time progresses.
Pasteable table:
dd = read.table(header = T, text = "    time   a    b   c   d   s   
k   w
1   18:41  'b d cat'       1     0   1   0    0   1
2   18:43  'b d dog'       1     0   1   0    0   1
3   18:47  'b d lamp'      0     0   0   0    0   1
4   18:51  'b s dog'       0     0   0   0    0   1
5   18:52  'b k cat'       0     0   0   0    0   1
6   18:57  'b c lion'      1     1   0   0    0   1
7   18:59  'b a dog'       1     1   0   0    0   1
8   19:23  'b w lamp'      0     1   0   0    0   0 
9   19:25  'b r cat'       0     1   0   0    0   0")
Desired Output:
    time   a           b      c
1   18:41  b d cat       1     0
2   18:43  b d dog       1     0 
3   18:47  b d lamp      0     0
4   18:51  b s dog       0     0
5   18:52  b k cat       0     0
6   18:57  b c lion      1     1
7   18:59  b a dog       1     1
8   19:23  b w lamp      0     1 
9   19:25  b r cat       0     1
Essentially, I am looking for a way to make column a's values operate as an on/off switch for the flag variables in b and c.
This is something that can be done quickly in Excel using relational formulas, but I would love an elegant R-based solution if one exists!
To solve the problem, the tricks I would need to incorporate (at least to my understanding) would be
- Come up with a formula and have it only apply to all rows except the first row.
- Refer to the row above if states are not changed based on the first two bits of logic.
Thank you in advance.
 
    