I'm sorry if this has been asked before. I have checked numerous questions about using if-else inside for loops to no avail. I must be misinterpreting the answers. I've edited my code multiple times to avoid different fatal errors and/or unanticipated outputs (incl. no change and/or only the else statement being evaluated).
What I want the loop to do: Check if Column A has a negative value and Column B is equal to 2. If both conditions are true, put a 0 in Column C; otherwise, copy the value from Column B as is to Column C. Ultimately, I also want to do the reverse - swap 0s for 2s when Column A is negative.
Thanks!
condition = c(0,1,2)
ppt1 = sample(condition, 124, replace = T) 
ES = rnorm(n = 124)
key = as.data.frame(cbind(ES,ppt1))
key$recoded = rep(NA, 124)
This creates a desired input like this:
key[1:6,]
          ES ppt1 recoded
1 -0.1893987    0      NA
2 -0.3840249    2      NA
3  0.7405880    2      NA
4 -1.1683384    0      NA
5  0.6675387    0      NA
6  0.3662369    2      NA
Here are my two ifelse attempts:
for(i in nrow(key)){ 
  ifelse((key$ES[i] < 0) && (key$ppt1[i] == 2),key$recoded[i] == 0,key$recoded[i] == key$ppt1[i]) 
}
for (i in 1:nrow(key)) {
  if(key$ES[i] < 0 && key$ppt1 == 2) {
    key$recoded[i] == 0
  } else {
    key$recoded[i] == "key"
  }
}
The desired outcome would be like this:
key[1:6,]
          ES ppt1 recoded
1 -0.9729880    0       2
2  0.5695559    1       1
3  1.1376975    2       2
4  0.8132510    2       2
5  0.8482997    1       1
6 -0.9434207    2       0
See how in rows 1 & 6, the $recoded column has swapped 0 and 2.
 
     
    