Re: [R] ifelse logic and multiple assignments

2010-03-10 Thread Bert Gunter
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-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
Behalf Of David Young
Sent: Wednesday, March 10, 2010 8:47 AM
To: r-help@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@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.

__
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.


Re: [R] ifelse logic and multiple assignments

2010-03-10 Thread David Winsemius


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) {  ops> } else

{  }
  }


 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.