Re: [R] IFELSE function XXXX

From: William Dunlap <wdunlap_at_tibco.com>
Date: Tue, 05 Apr 2011 14:09:59 -0700

> -----Original Message-----
> From: r-help-bounces_at_r-project.org
> [mailto:r-help-bounces_at_r-project.org] On Behalf Of Dan Abner
> Sent: Tuesday, April 05, 2011 12:58 PM
> To: r-help_at_r-project.org
> Subject: [R] IFELSE function XXXX
>
> Hello everyone,
>
> This IFELSE function call is not working properly. I do not
> receive an error
> message, but the actions are not executed conditional as I
> was hoping. Any
> assistance is appreciated.
>
> set.seed(12345)
> res1<-rbinom(10000,1,.1)
> rdata3<-transform(data.frame(res1),input1=rnorm(10000,50,10))
> data3
> #inducing correlation between res1 & input1
> ifelse(data3$res1==1,data3$input1<-data3$input1+10,data3$input
1<-data3$input1)

Avoid assignments in arguments to function calls, especially multiple assignments to the same object, except when you know what you are doing and want to write obscure code.

Change the above line to
  data3$input1 <- ifelse(data3$res1==1, data3$input1+10, data3input1)

There is nothing too special about ifelse here, you would get similar results if you put assignment statements in other function calls. The assignment gets evaluated just as things like log(12) or data3$input1+10 get evaulated when given as arguments. E.g.,   > x <- 1
  > cat(x <- 2:1, x <- 6:3, x <- 10:4, "\n")   2 1 6 5 4 3 10 9 8 7 6 5 4
  > x
  [1] 10 9 8 7 6 5 4
Which version of x you end up with depends on which argument the function evaluates last.
  > f <- function(arg1, arg2) arg2 + arg1   > f( x <- 7, x <- 101 )
  [1] 108
  > x
  [1] 7

An example of obscure code involving multiple assignments to one object is
  if (is.null(tms <- x$terms) && is.null(tms <- attr(x, "terms"))) {

     stop("Cannot find a list component or attribute called terms")   }
  tms # x$terms if it exists, otherwise attr(x, "terms") Since the order of evaluation of the 2 arguments to && is well defined (left then right, and the right won't be evaluated unless the left if TRUE), this produces a trustworthy answer. Most functions don't promise any particular order of evaluation.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

> data3
>
> Thank you,
>
> Dan
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



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. Received on Tue 05 Apr 2011 - 21:16:07 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 05 Apr 2011 - 23:10:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive