lundi 29 février 2016

How to combine different conditionals in R?

this is my reproducible example:

SMTscenes.ACC SMTscenes.RESP TrialType 
0             4              Old
0             3              Old
0             r              New
0             2              New
0             1              New
0             r              Old
0             3              Old
0             4              New

I started by using the following command to get rid of the "r" so that the next conditionals abide by numbers, then applying the conditional:

levels(df$SMTscenes.RESP)[levels(df$SMTscenes.RESP)=="r"] <- "5" df$SMTscenes.ACC <- ifelse(df$SMTscenes.RESP >= '3' & df$TrialType == 'Old', '1', '0') df[is.na(df)] <- 1 levels(df$SMTscenes.RESP)[levels(df$SMTscenes.RESP)=="5"] <- "r"

This yields

SMTscenes.ACC SMTscenes.RESP TrialType 
1             4              Old
1             3              Old
0             r              New
0             2              New
0             1              New
1             r              Old
1             3              Old
0             4              New

Here comes the tricky part: If I try to use the following (For 'New' now):

levels(df$SMTscenes.RESP)[levels(df$SMTscenes.RESP)=="r"] <- "5" df$SMTscenes.ACC <- ifelse(df$SMTscenes.RESP <= '2' & df$TrialType == 'New', '1', '0') df[is.na(df)] <- 1 levels(df$SMTscenes.RESP)[levels(df$SMTscenes.RESP)=="5"] <- "r"

then what happens is it reverses the previous step, obviously. Now, I've tried tweaking it in various ways, like mixing the conditionals, all to no avail. What I want the end result to be is this:

 SMTscenes.ACC SMTscenes.RESP TrialType 
1             4              Old
1             3              Old
0             r              New
1             2              New
1             1              New
1             r              Old
1             3              Old
0             4              New

So, any pointers on how to achieve this? I'd do it manually but there are thousands of rows of data... All help is appreciated.

Aucun commentaire:

Enregistrer un commentaire