I'm a fairly new R user and while I have a solution to my problem I'm wondering if there is a better one. In SAS it's common to use if/then logic along with a "do" statement to make several things happen. While I could do the same thing in R using a "for" loop, and "if" and {}, I've read that loops are less common in R and I wonder if I'm doing things ineffectively. Below I've used a "ifelse" function to make an assignment to the variable FUT.direction, but what I'd really like to do is make several assignments based on the same "ifelse" logic. Since the "ifelse" wants to create a vector the same size as the logical condition it doesn't seem obvious how I'd make several assignments and I worry about simply re-writing the logic for each assignment for fear of introducing errors that would be hard to find later. minitest$FUT.direction <- ifelse((minitest$FUT.lm.max.3.20.r2.price == max), ifelse((minitest$FUT.lm.max.3.20.slope.price > 0),1, 2), ifelse((minitest$FUT.lm.min.3.20.r2.price == max), ifelse((minitest$FUT.lm.min.3.20.slope.price > 0),1, 2), ifelse((minitest$FUT.lm.max.21.100.r2.avg.price == max), ifelse((minitest$FUT.lm.max.21.100.slope.avg.price > 0),3, 4), ifelse((minitest$FUT.lm.min.21.100.r2.avg.price == max), ifelse((minitest$FUT.lm.min.21.100.slope.avg.price > 0),3, 4), ifelse((NA == max),NA,NA) ) ) ) ) Should I just go ahead with the "for" loop or does someone know of a better way to use one set of logic (by observation) to make several assignments? Thanks in advance for any helpful advice. -- Best regards, David mailto:David.Young.Spain at gmail.com
I'm a fairly new R user and while I have a solution to my problem I'm wondering if there is a better one. In SAS it's common to use if/then logic along with a "do" statement to make several things happen. While I could do the same thing in R using a "for" loop, and "if" and {}, I've read that loops are less common in R and I wonder if I'm doing things ineffectively. Below I've used a "ifelse" function to make an assignment to the variable FUT.direction, but what I'd really like to do is make several assignments based on the same "ifelse" logic. Since the "ifelse" wants to create a vector the same size as the logical condition it doesn't seem obvious how I'd make several assignments and I worry about simply re-writing the logic for each assignment for fear of introducing errors that would be hard to find later. minitest$FUT.direction <- ifelse((minitest$FUT.lm.max.3.20.r2.price == max), ifelse((minitest$FUT.lm.max.3.20.slope.price > 0),1, 2), ifelse((minitest$FUT.lm.min.3.20.r2.price == max), ifelse((minitest$FUT.lm.min.3.20.slope.price > 0),1, 2), ifelse((minitest$FUT.lm.max.21.100.r2.avg.price == max), ifelse((minitest$FUT.lm.max.21.100.slope.avg.price > 0),3, 4), ifelse((minitest$FUT.lm.min.21.100.r2.avg.price == max), ifelse((minitest$FUT.lm.min.21.100.slope.avg.price > 0),3, 4), ifelse((NA == max),NA,NA) ) ) ) ) Should I just go ahead with the "for" loop or does someone know of a better way to use one set of logic (by observation) to make several assignments? Thanks in advance for any helpful advice. -- Best regards, David