[R] ifelse logic and multiple assignments

Bert Gunter gunter.berton at gene.com
Wed Mar 10 18:42:22 CET 2010


David:

The best advice I can give is: read "An Introduction to R" carefully, taking
particular note of the examples and the ideas of vectorization, factors,
lists, etc The bottom line is: yes, you can mimic the way SAS or other
ancient procedural languages do things in R (e.g. using nested ifelse()
constructions), but a "cleaner," more readable and perhaps more efficient
way is to create a factor or factors that delineate the different categories
-- perhaps using cut(),split() or other R internal function designed for
this purpose -- and then using apply type functions (e.g. lapply(),
tapply(),ave(), by() or cognate functions in the plyr package, which you may
find easier to learn and use ) to produce summaries for your various
subsets. Indeed,what appears to be your effort to create group codings below
may be wholly unnecessary. As David Winsemius noted, your failure to follow
the posting guide makes it difficult to divine your intent.

The paradigms underlying R -- as a functional language designed on the
abstraction of whole object manipulation -- are exposited in a growing
number of books on R,many of them listed on the CRAN website. (Or you can
Google, search Amazon, etc. on e.g. "R Statistical Language"). My point is:
to use R effectively, you need to purge SAS from your brain and think about
things in a new way. Tough to do, I know, but your efforts will be
generously rewarded.

As always, contrary views welcome.

Bert Gunter
Genentech Nonclinical Statistics


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of David Young
Sent: Wednesday, March 10, 2010 8:47 AM
To: r-help at r-project.org
Subject: [R] ifelse logic and multiple assignments

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

______________________________________________
R-help at 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.



More information about the R-help mailing list