title: add values to a column based on conditions on other columns with repeated measures
body: <p>I am pretty much new to R and am trying to prepare my data set in Rstudio for further analysis. I will be appreciative if anyone can help me find a solution for my code. My data is called m8. This is what I have done so far. But it only returns the max value between &quot;U1High&quot;, &quot;U1Low&quot; and &quot;U1Med&quot; in the entire column &quot;Q2&quot;. This is not What I want.</p>
  <pre><code>    for (i in 1:length(m9[ , &quot;condition&quot;])) {
    if(m9[m9$condition == &quot;U1High&quot;, ] ||
       m9[m9$condition == &quot;U1Med&quot;, ]) {
      if (m9[m9$condition == &quot;U1High&quot;, ][ , &quot;Subject&quot;] == m9[m9$condition == &quot;U1Med&quot;, ][ , &quot;Subject&quot;]) {
        for (j in 1:length(max(m9[m9$condition == &quot;U1High&quot;, ][ , &quot;Q2&quot;], 
                               m9[m9$condition == &quot;U1Med&quot;, ][ , &quot;Q2&quot;]))) {
          m9$desktop.adjusted[i] = 1
        }
      }
    }
  }
  </code></pre>
  <p>I am trying to add values to the last column named &quot;desktop.adjusted&quot; based on two conditions on other columns. I have repeated measurements for each subject. I need to select “U1High”, “U1Low” and “U1Med” from the Condition column within each subject. This means that the subject number for “U1High”, “U1Med” and “U1Low” have to be equal. Then pick the max value for the correspondent rows on the Q2 column. Then turn the max value to 1 and put it on the desktop.adjusted column. Also the min value to 3 and add it to the desktop.adjusted column. The number between min and max has to be assigned as 2 and added to the &quot;desktop.adjusted&quot; column.</p>
  <p>Any suggestions and help would be appreciated.</p>