I am trying to do an if/then replacement (recoding) of values row by row (looping over rows), based on one the values of or more columns in those rows. I've looked at a lot of prior examples here and elsewhere (R help) but haven't been able to get very far.
Here is an example data set:
> set.seed(1234)
> let<-c("AB","AA","BB")
> df <- data.frame(rbind(x1=c(12,"DF1",sample(let,6,TRUE)),x2=c(12,"HA.1",sample(let,6,TRUE)),x3=c(21,"DF1",sample(let,6,TRUE)),x4=c(12,"AS.2",sample(let,6,TRUE))
+ ))
> df
X1 X2 X3 X4 X5 X6 X7 X8
x1 12 DF1 AB AA AA AA BB AA
x2 12 HA.1 AB AB AA AA BB AA
x3 21 DF1 AB BB AB BB AB AB
x4 12 AS.2 AB AB AB AB AB AB
I would like to conditionally change the coding (replace) values in columns 3:8 (from X3 through X8) based on values in X1 and X2 using if/then. 'AB' becomes 1 if X1=12 AND X2=DF1, 'AA' becomes 2 if X1=12 AND X2=DF1, 'BB' becomes 3 if X1=12 and X2=DF1 etc. There will be many other (nested?) if statements to add to complete this specific case, but I am not sure how to approach even the most basic aspect of this script: how to condition the replacement of values in columns 3:8 based on the column 1 value (and also column 2 or more columns) at a given row.
So, looping over each row, I would test if the value in X2 = DF1 and X1=12 (for example), and if so in both cases, change values of AB to 1, AA to 2, and BB to 3...
for(i in 1:nrow(df)){
if((df$X2[i]=="DF1") & (df$X1[i]=12)) {
ifelse(df[i,3:8] == "AB", 1, ifelse(df[i,3:8]=="AA", 2,ifelse(df[i,3:8]=="BB",3,"NA")))}
else{}
}
Now...this appears to do nothing - no changes to df
and no warnings. But the ifelse
statements work when I specify the row (4):
> ifelse(df[4,3:8] == "AB", 1, ifelse(df[4,3:8]=="AA", 2,ifelse(df[4,3:8]=="BB",3,"NA")))
X3 X4 X5 X6 X7 X8
x4 "1""3""1""1""1""2"> df[4,3:8]
X3 X4 X5 X6 X7 X8
x4 AB BB AB AB AB AA
So it must be something in the initial if &
? Do I need to have something in my else
clause?
And of course, my 'real' world use case is more complicated as each different value in X1 or X2 will require different if/then statements to recode the values in colum 3:8
Anyway - am I even approaching this correctly? Would a look up table work better? I would be setting up additional nested if/& statements for each combination of values for X1 and X2. It will be ugly, but if I can get the nested if statements to work, then at least I can get there.
Thanks for any suggestions!