0

I have a dataframe FLANK:

Tar_RL  Tar_UD  Resp RT     Button_Rel  Accur            
LEFT    UP      201  12582  1439        miss
RIGHT   DOWN    203  12389  1440        miss
RIGHT   UP      204  10748  1280        miss
RIGHT   DOWN    203  11056  1760        miss
RIGHT   DOWN    203  6090   1680        miss
LEFT    DOWN    202  5743   1440        hit
LEFT    DOWN    202  5671   1520        hit
LEFT    UP      201  5378   1520        hit
RIGHT   UP      204  5032   1360        hit
RIGHT   DOWN    203  5038   1440        hit
RIGHT   UP      204  4399   1360        hit
RIGHT   DOWN    203  4273   1849        hit
RIGHT   DOWN    203  5087   1683        hit
RIGHT   UP      204  4453   1760        hit

The combinations of Tar_RL and TAR_UD are following: Left + Up is coded as 201, LD - 202, RD - 203, RU - 204.

I wrote a code to map the Tar_RL and Tar_UD combinations in a new column Corr_Resp:

FLANK$Corr_Resp <- 0 

for (i in  1:nrow(FLANK)) {
 if(FLANK$Tar_RL[i]<= 'LEFT' & FLANK$Tar_UD[i]<= 'UP'){
   FLANK$Corr_Resp[i] <- 201
 }
 else if (FLANK$Tar_RL[i]<= 'LEFT' & FLANK$Tar_UD[i]<= 'DOWN'){
   FLANK$Corr_Resp[i] <- 202
 }
 else if(FLANK$Tar_RL[i]<= 'RIGHT' & FLANK$Tar_UD[i]<= 'DOWN'){
   FLANK$Corr_Resp[i] <- 203
 }
   else if (FLANK$Tar_RL[i]<= 'RIGHT' & FLANK$Tar_UD[i]<= 'UP'){
   FLANK$Corr_Resp[i] <- 204
}
} 

However, it gives me this kind of output where values of 202 (combinations of LD) are missing:

Tar_RL  Tar_UD  Resp RT     Button_Rel  Accur  Corr_Resp
LEFT    UP      201  12582  1439        miss   201
RIGHT   DOWN    203  12389  1440        miss   203
RIGHT   UP      204  10748  1280        miss   204
RIGHT   DOWN    203  11056  1760        miss   203
RIGHT   DOWN    203  6090   1680        miss   203
LEFT    DOWN    202  5743   1440        hit    201
LEFT    DOWN    202  5671   1520        hit    201
LEFT    UP      201  5378   1520        hit    201
RIGHT   UP      204  5032   1360        hit    204
RIGHT   DOWN    203  5038   1440        hit    203
RIGHT   UP      204  4399   1360        hit    204
RIGHT   DOWN    203  4273   1849        hit    203
RIGHT   DOWN    203  5087   1683        hit    203
RIGHT   UP      204  4453   1760        hit    204

What is wrong with my code?

5
  • Why you try <=(less or equal) with character? may be you need ==? Commented Nov 5, 2015 at 14:02
  • Thanks, that solved the problem! Commented Nov 5, 2015 at 14:04
  • you can do it without for FLANK$Corr_Resp[FLANK$Tar_RL== 'LEFT' & FLANK$Tar_UD== 'UP']=201 and for other combinations Commented Nov 5, 2015 at 14:06
  • what about to recode the result of with(FLANK, paste0(Tar_RL, Tar_UD)) ? Commented Nov 5, 2015 at 14:11
  • thanks, I've also tried your suggestion and it also worked Commented Nov 5, 2015 at 14:40

3 Answers 3

3

You are using <= relational operator on string. Strings are compared in lexicographical order by relational operators.

So, "DOWN" <= "UP" will return TRUE.

Now, in your code,

first if branch runs when Tar_RL[i] is "LEFT" and Tar_UD[i] is either "UP" or "DOWN".

second if branch runs when Tar_RL[i] is "LEFT" and Tar_UD[i] is "DOWN".

This condition is already satisfied in first block. So second block will never run. And you won't get 202 in Corr_Resp column.

either change the order of these two if blocks, i.e.,

if(FLANK$Tar_RL[i]<= 'LEFT' & FLANK$Tar_UD[i]<= 'DOWN'){
    FLANK$Corr_Resp[i] <- 201
}
else if (FLANK$Tar_RL[i]<= 'LEFT' & FLANK$Tar_UD[i]<= 'UP'){
    FLANK$Corr_Resp[i] <- 202
}

or a much much better and easily understandable solution is to use == operator to check if two strings are same.

Sign up to request clarification or add additional context in comments.

Comments

2

there are typo error replace <= on ==

but better do it without loop

 FLANK$Corr_Resp[FLANK$Tar_RL== 'LEFT' & FLANK$Tar_UD== 'UP']=201
FLANK$Corr_Resp[FLANK$Tar_RL== 'LEFT' & FLANK$Tar_UD== 'DOWN']=202
FLANK$Corr_Resp[FLANK$Tar_RL== 'RIGHT' & FLANK$Tar_UD== 'DOWN']=203
FLANK$Corr_Resp[FLANK$Tar_RL== 'RIGHT' & FLANK$Tar_UD== 'UP']=204

Comments

1
as.numeric(factor(with(FLANK, paste0(Tar_RL, Tar_UD)),
 levels=c("LEFTUP", "LEFTDOWN", "RIGHTDOWN", "RIGHTUP")))+200

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.