Q:

Strange behavior of switch function in R

I have a strange problem of switch function in R.
The code is very simple ,but I do not understand the behavior.
Example:OK
a=1

switch(a,             
       "1" = print("one"),   
       "2" = print("two"),  
       print("?")            
        ) 

Correct
a=1 >>> [1] "one"

Correct
a=2 >>> [1] "two"

Corrct
a=3 >>> [1] "?"

4) Strange
a=0 >>> Nothing printed.
a=-1 >>> Nothing printed.
Another example is also strange
a=-1

switch(a,             
       "1" = print("one"),   
       "-1" = print("minus"),  
       print("?")            
        ) 

Nothing printed!
Why the case of minus value causes the unexpected behavior?

A:

You should force the value of a to a character before comparing it. It seems not to process negative integers as expected. But with as.character() it works just fine.
a=-1

switch(as.character(a),             
       "1" = print("one"),   
       "-1" = print("minus"),  
       print("?")            
)

