0

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 "U1High", "U1Low" and "U1Med" in the entire column "Q2". This is not What I want.

    for (i in 1:length(m9[ , "condition"])) {
  if(m9[m9$condition == "U1High", ] ||
     m9[m9$condition == "U1Med", ]) {
    if (m9[m9$condition == "U1High", ][ , "Subject"] == m9[m9$condition == "U1Med", ][ , "Subject"]) {
      for (j in 1:length(max(m9[m9$condition == "U1High", ][ , "Q2"], 
                             m9[m9$condition == "U1Med", ][ , "Q2"]))) {
        m9$desktop.adjusted[i] = 1
      }
    }
  }
}

I am trying to add values to the last column named "desktop.adjusted" 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 "desktop.adjusted" column.

Any suggestions and help would be appreciated.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
Sohrab
  • 1
  • 1
    Please share a reproducible sample of your data - `dput` is the nicest way to do this, `dput(m9[1:10, ])` will give us a copy/pasteable version of the frist 10 rows of `m9` including all class and structure information. Pick a more suitable subset if the first 10 rows don't illustrate your problem well. And please share your desired output for the sample input - that will make it much easier to confirm our understanding of your question. – Gregor Thomas Dec 14 '20 at 02:12
  • And please do use `dput()` to share data as text. Pictures of data can help us understand a little bit, but we cannot test and demonstrate a solution on a picture. Use `dput()` as I describe above. If you need more help making your example reproducible, [see this FAQ on the subject](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Gregor Thomas Dec 14 '20 at 04:00

0 Answers0