[R] ifelse...

Duncan Murdoch murdoch.duncan at gmail.com
Thu Jan 16 15:36:48 CET 2014


On 16/01/2014 8:46 AM, ONKELINX, Thierry wrote:
> You want
> y <- ifelse(x == 'a', 1,  2)
>
or use if, rather than ifelse, i.e.

if (x == 'a') {
   y <- 1
} else {
   y <- 2
}

ifelse() is mainly used when you want to work with whole vectors of 
decisions, e.g.

x <- 1:10
ifelse(x > 5, 1, 0)

Duncan Murdoch




More information about the R-help mailing list