title: Elegant way to change a variable according to several conditions?
body: <p>I'm trying to change the variable &quot;exposure&quot; according to multiple conditions.</p>
  <p><strong>For example:</strong> If stimulus_content is &quot;neg&quot;, and if condition is &quot;neg&quot;, and if set is &quot;A&quot;, then the content of the variable &quot;exposure&quot; should be changed to &quot;long&quot; for the lines in which stimulus_no is either X1, X2, ... or X5. The variable &quot;exposure should be changed to &quot;short&quot; for the lines in which stimulus_no is either X6, X7, ... or X10. And so on...</p>
  <p>I hope the code below makes the problem more clear.</p>
  <p>First, this is the approximate dataset:</p>
  <pre><code>n &lt;- 6
  dataset &lt;- data.frame(
  participant = rep(1:n, each=40),
  condition = rep(c(&quot;pos&quot;,&quot;neg&quot;), each=40),
  set = rep(c(&quot;A&quot;,&quot;B&quot;), each=40),
  stimulus_content = rep(c(&quot;pos&quot;,&quot;neg&quot;), each=2),
  stimulus_no = rep(c(&quot;X1&quot;,&quot;X10&quot;,&quot;X11&quot;,&quot;X12&quot;,&quot;X13&quot;,&quot;X14&quot;,&quot;X15&quot;,&quot;X16&quot;,&quot;X17&quot;,&quot;X18&quot;,&quot;X19&quot;,&quot;X2&quot;,&quot;X20&quot;,&quot;X3&quot;,&quot;X4&quot;,&quot;X5&quot;,&quot;X6&quot;,&quot;X7&quot;,&quot;X8&quot;,&quot;X9&quot;), each=2),
  exposure = NA)
  </code></pre>
  <p><strong>The first thing we tried is via a loop.</strong> For the sake of simplicity, only one part of the loop is included. It doesn't return an error but it also doesn't do anything.</p>
  <pre><code>for (i in 1:length(longdat[,1])){
    if(longdat[i,&quot;stimulus_content&quot;] == &quot;pos&quot;) { 
      if(longdat[i,&quot;condition&quot;] == &quot;pos&quot;) {
        if(longdat[i,&quot;set&quot;] == &quot;A&quot;) {     
          for(stimulus_no in c(&quot;X1&quot;,&quot;X2&quot;,&quot;X3&quot;,&quot;X4&quot;,&quot;X5&quot;)){longdat[i,&quot;exposure&quot;] == &quot;long&quot;}
          for(stimulus_no in c(&quot;X6&quot;,&quot;X7&quot;,&quot;X8&quot;,&quot;X9&quot;,&quot;X10&quot;)){longdat[i,&quot;exposure&quot;] == &quot;short&quot;}
          for(stimulus_no in c(&quot;X11&quot;,&quot;X12&quot;,&quot;X13&quot;,&quot;X14&quot;,&quot;X15&quot;,&quot;X16&quot;,&quot;X17&quot;,&quot;X18&quot;,&quot;X19&quot;,&quot;X20&quot;)){longdat[i,&quot;exposure&quot;] == &quot;none&quot;}
        } else { #for condition = pos and set != A            
          for(stimulus_no in c(&quot;X11&quot;,&quot;X12&quot;,&quot;X13&quot;,&quot;X14&quot;,&quot;X15&quot;)){longdat[i,&quot;exposure&quot;] == &quot;long&quot;}
          for(stimulus_no in c(&quot;X16&quot;,&quot;X17&quot;,&quot;X18&quot;,&quot;X19&quot;,&quot;X20&quot;)){longdat[i,&quot;exposure&quot;] == &quot;short&quot;}
          for(stimulus_no in c(&quot;X1&quot;,&quot;X2&quot;,&quot;X3&quot;,&quot;X4&quot;,&quot;X5&quot;,&quot;X6&quot;,&quot;X7&quot;,&quot;X8&quot;,&quot;X9&quot;,&quot;X10&quot;)){longdat[i,&quot;exposure&quot;] == &quot;none&quot;}
        }
      }
    }
  }
  </code></pre>
  <p>Next, we tried via <strong>mutate and case_when</strong>. This code does exactly what it's supposed to but it's almost 100 lines long! Please find an excerpt below.</p>
  <pre><code>longdat2 &lt;- longdat %&gt;%
    mutate(exposure = case_when(
      # Condition pos, set A
      stimulus_no==&quot;X1&quot; &amp; stimulus_content==&quot;pos&quot; &amp; condition==&quot;pos&quot; &amp; set==&quot;A&quot; ~ &quot;long&quot;,
      stimulus_no==&quot;X2&quot; &amp; stimulus_content==&quot;pos&quot; &amp; condition==&quot;pos&quot; &amp; set==&quot;A&quot; ~ &quot;long&quot;,
      # ...
      stimulus_no==&quot;X9&quot; &amp; stimulus_content==&quot;pos&quot; &amp; condition==&quot;pos&quot; &amp; set==&quot;A&quot; ~ &quot;short&quot;,
      stimulus_no==&quot;X10&quot; &amp; stimulus_content==&quot;pos&quot; &amp; condition==&quot;pos&quot; &amp; set==&quot;A&quot; ~ &quot;short&quot;,
      stimulus_no==&quot;X11&quot; &amp; stimulus_content==&quot;pos&quot; &amp; condition==&quot;pos&quot; &amp; set==&quot;A&quot; ~ &quot;none&quot;,
      # ... accordingly for condition pos and set B, and for condition neg and set A
      # and eventually for condition neg and set B
      stimulus_no==&quot;X18&quot; &amp; stimulus_content==&quot;neg&quot; &amp; condition==&quot;neg&quot; &amp; set==&quot;B&quot; ~ &quot;short&quot;,
      stimulus_no==&quot;X19&quot; &amp; stimulus_content==&quot;neg&quot; &amp; condition==&quot;neg&quot; &amp; set==&quot;B&quot; ~ &quot;short&quot;,
      stimulus_no==&quot;X20&quot; &amp; stimulus_content==&quot;neg&quot; &amp; condition==&quot;neg&quot; &amp; set==&quot;B&quot; ~ &quot;short&quot;,
    )
  )
  </code></pre>
  <p>If someone manages to spot the error in the loop or could tell me a more succinct version of the second (or first) option, I'd be very grateful!</p>
  <p>Thanks a lot in advance!</p>