Or convert the EXPR to character : x <- 5 switch(as.character(x), '2' = 3, 'default')
On Wed, Oct 6, 2010 at 9:57 AM, David Winsemius <dwinsem...@comcast.net>wrote: > > 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, raje...@cse.iitm.ac.in < >> raje...@cse.iitm.ac.in> wrote: >> >> >>> Hi, >>> >>> Is there a c++ equivalent default case in the switch command? >>> [[alternative HTML version deleted]] >>> >> > > > -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
______________________________________________ R-help@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.