title: R: Assigning variable in data.table using logical statement inside "()" in ifelse function
body: <p>In the question <a href="https://stackoverflow.com/questions/29150682/defining-variable-by-logical-subseting-on-time-interval-in-data-table">Defining variable by logical subseting on time interval in data.table</a> I asked for help with assgning a "state" variable based on a time code being between to events, namely <code>event==1</code> and <code>event==2</code>.</p>
  
  <p>The solution makes use of an <code>ifelse</code> function where the logical test checks wether a time variable is between the timevalue of the starting point and end point.</p>
  
  <p>The problem is if I want to group the logical statements in the <code>ifelse</code> function. So first evaluating and OR-statement and then evaluating a AND-statement. For concretness i have the following <code>data.table</code>.</p>
  
  <pre><code># Defining variables and data.table
  id &lt;- rep(LETTERS[1:3],each=5)
  set.seed(123)
  event &lt;- c(sample(c(0,1),2,F),sample(c(0,0,2),3,F),
             sample(c(0,1),2,F),sample(c(0,0,2),3,F),
             sample(c(0,1),2,F),sample(c(0,0,2),3,F))
  event[event==2] &lt;- sample(c(2,3),3,T)
  state &lt;- "NULL"
  time &lt;- c(apply(matrix(runif(3*5),5,3),2,cumsum))
  DT &lt;- data.table(id,event,state,time) 
  DT[14,] &lt;- DT[13,]
  DT[14,event:=3]
  </code></pre>
  
  <p>Which produces this <code>data.table</code>:</p>
  
  <pre><code>    id event state      time
   1:  A     0  NULL 0.3279207
   2:  A     1  NULL 1.2824244
   3:  A     0  NULL 2.1719637
   4:  A     3  NULL 2.8647671  &lt;- Event 2 or 3 marks the end point
   5:  A     0  NULL 3.5052739
   6:  B     0  NULL 0.9942698
   7:  B     1  NULL 1.6499756
   8:  B     2  NULL 2.3585060  &lt;- Event 2 or 3 marks the end point
   9:  B     0  NULL 2.9025721
  10:  B     0  NULL 3.4967141
  11:  C     1  NULL 0.2891597
  12:  C     0  NULL 0.4362734
  13:  C     2  NULL 1.3992976  &lt;- Here both 2 and 3 appear at the same endpoint 
  14:  C     3  NULL 1.3992976  &lt;- Here both 2 and 3 appear at the same endpoint 
  15:  C     0  NULL 2.9923019
  </code></pre>
  
  <p>I want to assign the value 1 to the state variable for all observations between the start event (<code>event==1</code>) and the end point (<code>event==2</code> OR <code>event==3</code> OR BOTH). So the correct result looks like this:</p>
  
  <pre><code>    id event state      time
   1:  A     0  NULL 0.3279207
   2:  A     1     1 1.2824244
   3:  A     0     1 2.1719637
   4:  A     3     1 2.8647671
   5:  A     0  NULL 3.5052739
   6:  B     0  NULL 0.9942698
   7:  B     1     1 1.6499756
   8:  B     2     1 2.3585060
   9:  B     0  NULL 2.9025721
  10:  B     0  NULL 3.4967141
  11:  C     1     1 0.2891597
  12:  C     0     1 0.4362734
  13:  C     2     1 1.3992976
  14:  C     3     1 1.3992976
  15:  C     0  NULL 2.9923019
  </code></pre>
  
  <p>My first try was this code:</p>
  
  <pre><code>DT[,state:=ifelse(time&gt;=time[event==1] &amp; (time&lt;=time[event==2] | time&lt;=time[event==3]),1,state),by=id]
  </code></pre>
  
  <p>which gives the following error message:</p>
  
  <pre><code>Error in `[.data.table`(DT, , `:=`(state, ifelse(time &gt;= time[event ==  : 
  Type of RHS ('logical') must match LHS ('character'). To check and coerce would 
  impact performance too much for the fastest cases. Either change the type of the target 
  column, or coerce the RHS of := yourself (e.g. by using 1L instead of 1)
  </code></pre>
  
  <p>This line of code produces the correct result,</p>
  
  <pre><code>DT[,state:=ifelse(time&gt;=time[event==1] &amp; time&lt;=time[event==2 | event==3],1,state),by=id]
  </code></pre>
  
  <p>but it produces a warning when the logical statement <code>time&lt;=time[event==2 | event==3]</code> has length greater the 1. So it not an elegant solution, since it looks like a mistake.</p>
  
  <p>How can i assign the value 1 to the state-variable, IF the time is between the start point and end point, where the end point is defined by a OR-statement like in my first try.</p>
  
  <p>Thanks a lot.</p>