[R] Why doesn't this work ?

Phil Spector spector at stat.berkeley.edu
Thu Mar 17 04:57:59 CET 2011


Eric -
    What you mean to say is

> t <- 3
> z <- ifelse(t %in% c(1,2,3),1,0)
> z
[1] 1
> t <- 4
> z <- ifelse(t %in% c(1,2,3),1,0)
[1] 0

Expressions don't recalculate themselves when you change
the value of a variable that they use.  For that, you 
would need a function:

> makez = function(t)ifelse(t %in% c(1,2,3),1,0)
> makez(3)
[1] 1
> makez(4)
[1] 0


 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu



On Wed, 16 Mar 2011, eric wrote:

> Why doesn't this work and is there a better way ?
>
> z <-ifelse(t==1 || 2 || 3, 1,0)
> t <-3
> z
> [1] 1
> t <-4
> z
> [1] 1
>
> trying to say ...if t == 1 or if t== 2 or if t ==3 then true, otherwise
> false
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Why-doesn-t-this-work-tp3383656p3383656.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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