0

I hope someone could take a look at the if statement below and tell my how I should change it to get the results I want.

Essentially, I want the code to (1) run through (iterate over) every row in the data frame beh_data, and (2) if the character in the "Cue" column is identical to that in the "face1" column, I want to (3) take the value from the "Enc_trials.thisRepN" column, and (4) assign it to the "scr_of_trial" column. If they are not the same, I want to assign an NA to the "scr_of_trial" column.

Currently, the code runs, but assings NA to every row in the "scr_of_trial" column.

Can anyone tell me why?

Here is the code:

j <- 1
i = as.character(beh_data$Cue[1:1])

for (x in 1:NROW(beh_data$Cue)) {
  if (beh_data$Cue[j] == beh_data$face1[j]) {
    beh_data$scr_of_trial[j] <- beh_data$Enc_trials.thisRepN[j]

j <- j + 1
i = as.character(beh_data$Cue[1:1+j])
}

else {
  beh_data$scr_of_trial[j] <- NA

  j <- j + 1
  i = as.character(beh_data$Cue[1:1+j])
  
  next

 }
}  
  • Please add a minimal representation of your data including an output example. Otherwise its impossible to give you solid advice other than maybes. – Andre Wildberg Feb 25 '21 at 23:15

1 Answers1

0

Shift your thinking to whole-vectors-at-a-time.

A few techniques:

  1. ifelse; while it works fine here, realize that ifelse has issues with class.

    beh_data$scr_of_trial <- ifelse(beh_data$Cue == beh_data$face1,
                                    beh_data$Enc_trials.thisRepN, NA_character_)
    
  2. replace; similar functionality, no class problem:

    replace(beh_data$Enc_trials.thisRepN, beh_data$Cue != beh_data$face1, NA_character_)
    
  3. Use what I call an "indicator variable":

    ind <- beh_data$Cue == beh_data$face1
    beh_data$scr_of_trial <- NA_character_
    beh_data$scr_of_trial[ind] <- beh_data$Enc_trials.thisRepN
    

No for loops, just whole vectors at a time.

When reasonable, I tend to use class-specific NA types like NA_character_; while base R's functions will happily up-convert for you to whatever class you have, many other dialects within R (e.g., dplyr, data.table) are less permissive. It's a little declarative programming, a little style, perhaps a little snobbery, I don't know ...

(This is all untested on actual data.)

r2evans
  • 141,215
  • 6
  • 77
  • 149