[BioC] asking for some code
Adaikalavan Ramasamy
ramasamy at cancer.org.uk
Thu Mar 10 15:04:33 CET 2005
Thanks Kasper. I did not know R's switch now has a default value which
is very convenient for many things I am doing. And the use of -Inf and
Inf is excellent suggestion as well.
I would like to add that the values of 1, 2, ... have to be characters
as well. Otherwise you have syntax error. For example :
x <- 5
switch(as.character(x),
"1" = "one",
"2" = "two",
"Everything else")
Regards, Adai
On Wed, 2005-03-09 at 18:06 +0100, Kasper Daniel Hansen wrote:
> On Wed, Mar 09, 2005 at 02:41:36PM +0000, Adaikalavan Ramasamy wrote:
> > sapply( x, my.switch.fn )
> > [1] 1.2655087 1.3721239 1.5728534 2.9082078 0.2016819
> >
> >
> > In the case of 2 breakpoints, my example does not look much shorter than
> > the if-else clause below. The drawback of my suggestion is that you need
> > to know the min and max occuring values values (in this case 0 and 1)
> > and switch does not have a default value.
>
> You can use Inf/-Inf to get around not knowing he min and max, and you
> can indeed give a further argument to switch such like
> switch(x, 1 = "One",
> 2 = "Two",
> "Everything else")
>
> Kasper
>
More information about the Bioconductor
mailing list