On Mar 10, 2010, at 11:47 AM, David Young wrote:

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),

My wet-ware interpreter says that condition would never be satisfied and probably not processed when you expected it to be, since any conditions satisfying that antecedent (minitest$FUT.lm.min. 3.20.r2.price == max) would have already been processed above that line and what follows would be ignored, so I suppose the fact that the positive consequent is the same is consistent at least. But if you were hoping that anything following that would be processed might be misplaced because of the nested fashion in which you wrote it.

ifelse((minitest$FUT.lm.min.3.20.slope.price > 0),1, 2), #redundant ifelse((minitest$FUT.lm.max.21.100.r2.avg.price == max), # repeated below
       ifelse((minitest$FUT.lm.max.21.100.slope.avg.price > 0),3, 4),
ifelse((minitest$FUT.lm.min.21.100.r2.avg.price == max), #likewise redundant ifelse((minitest$FUT.lm.min.21.100.slope.avg.price > 0),3, 4), #likewise redundant
         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?

With so much unnecessary code and no information about what the real problem structure looks like, it is _really_ hard to know. Certainly radical simplification of the above would be possible. It is also possible that you would gain some flexibility by looking at the match function.

SAS has an implicit for-loop in its DATA steps and the corresponding, possibly inefficient, R construction would be:

for (idx in seq_along(dtfrm) { if (cond-on-idx-ed-values) { <multiple- ops> } else
                                            { <alternate-ops> }
                              }

 Thanks in advance for any helpful advice.

My advice. Follow the Posting Guide and create a sample dataset and explain in ordinary English what you are attempting and what you want the results to be.


--
Best regards,
David

--

David Winsemius, MD
West Hartford, CT

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to