[R] c++ equivalent 'default' case in switch

David Winsemius dwinsemius at comcast.net
Wed Oct 6 14:57:16 CEST 2010


On Oct 6, 2010, at 8:42 AM, Henrique Dallazuanna wrote:

> Yes,
>
> switch('d', b = 1, 'default')
>

?switch

For numeric arguments, the OP would need to construct a wrapper that  
tested for a NULL return.

 > switch(2, b = 1, 'default')
[1] "default"
 > switch(4, b = 1, 'default')
 > is.null(switch(4, b = 1, 'default'))
[1] TRUE

switchnum <- function(EXPR, ...){
+          if (!is.numeric(EXPR)) {switch(EXPR, ...)} else{
+                if (!is.null(switch(EXPR, ...))) {switch(EXPR, ...)}  
else{
+                   "default"} } }
 > switchnum(4, b = 1, 'default')
[1] "default"

The outer test for numericity may be superfluous.

-- 
David
>
> On Wed, Oct 6, 2010 at 8:43 AM, rajeshj at cse.iitm.ac.in <
> rajeshj at cse.iitm.ac.in> wrote:
>
>>
>> Hi,
>>
>> Is there a c++ equivalent default case in the switch command?
>>       [[alternative HTML version deleted]]



More information about the R-help mailing list