Re: [R] switch and factors

2011-03-14 Thread baptiste auguie
Hi, It is indeed documented but I'd misinterpreted it, my mistake. I made the wrong assumption that switch would have a factor method which would use the string levels, but that just isn't the case. Thanks for the clarification anyway, baptiste On 14 March 2011 22:22, Allan Engelhardt wrote: >

Re: [R] switch and factors

2011-03-14 Thread Joshua Wiley
Dear Baptiste, On Tue, Mar 8, 2011 at 6:02 PM, baptiste auguie wrote: > Dear list, > > Reading the help page for ?switch didn't give me more than a hint at > what's going on here, > > x = 5 > y = 2 > > foo <- function(a="x"){ >     switch(a, "x" = x, >                     "y" = y) > } > > foo(fac

Re: [R] switch and factors

2011-03-14 Thread Allan Engelhardt
Maybe I am misunderstanding you, but I think it is documented? The help page says switch(EXPR, ...) [...] If the value of ‘EXPR’ is not a character string it is coerced to integer. [...] Since is.character( factor('x', levels=c('y', 'x')) ) [1] FALSE you get as.integer( factor('x', leve

[R] switch and factors

2011-03-08 Thread baptiste auguie
Dear list, Reading the help page for ?switch didn't give me more than a hint at what's going on here, x = 5 y = 2 foo <- function(a="x"){ switch(a, "x" = x, "y" = y) } foo(factor('x', levels=c('y', 'x'))) # 2 It seems that switch, when given a factor, uses the numeri