Search code examples
rspss

Recode several variables into new variables in spss or r


I have data from an go/no go experiment with 200 variables.

  1. trial1 to trial100 where 0 describes that it is a no go trial and 1 to 15 that it is a go trial. No go trials and go trials are randomized.
  2. answer1 to answer100 recordes the reactiontime (with -1 as missing)

I would like to recode the reactiontime into new variables GoTrialRT1 to 100 (if the trial is 1 to 15) and NoGoTrialRT1 to 100 if the trial is 0. Is it a way to do this with spss syntax or in r?

ID trial1 trial2 trial3 trial.. answer1 answer2 answer3 answer..
1 3 5 14 .. 489 363 700 ..
2 0 6 15 .. 368 967 1201 ..
3 8 0 10 .. -1 671 890 ..
4 2 7 0 .. 358 353 -1 ..

Solution

  • The following code will put the value of answerX in NoGoTrialRTX if the trialX value was 0 or in GoTrialRTX if the trialX value was larger than 0 (leaving the other one empty). If trialX was missing both NoGoTrialRTX and GoTrialRTX will remain empty:

    do repeat trl = trial1 to trial100
          /ans = answer1 to answer100
          /Ng = NoGoTrialRT1 to NoGoTrialRT100
          /Yg = GoTrialRT1 to GoTrialRT100.
    if trl = 0 Ng=ans.
    if trl > 0 Yg=ans.
    end repeat.