[BioC] asking for some code

Adaikalavan Ramasamy ramasamy at cancer.org.uk
Wed Mar 9 15:41:36 CET 2005


A slightly more intuitive way is to cut() the variable returning labels,
which you can then feed into switch().

# simulate data
 set.seed(1)
 x <- runif(5)
 x
[1] 0.2655087 0.3721239 0.5728534 0.9082078 0.2016819


Let us say that n=0.25 and m=0.6.

my.switch.fn <- function(x ){

   y <- cut( x, breaks=c(0, 0.25, 0.6, 1), labels=FALSE )
   
   z <- switch(y, 
               "1" = x,
               "2" = x + 1,
               "3" = x + 2
               )
   return(z)
}

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.

Regards, Adai


On Wed, 2005-03-09 at 05:56 -0500, Sean Davis wrote:
> Taking this apart:
> 
> to find the Q5(quantile):
>    See ?quantile
> 
> for each row:
>    See ?apply
> 
> if x<n {
>    do stuff
> } else if ((x>n) & (x<m)) {
>    do other stuff
> } else {
>    do other other stuff
> }
> 
> Sean
> 
> On Mar 9, 2005, at 4:15 AM, zhihua li wrote:
> 
> > Dear netters,
> >
> > Now I have a matrix at hand. For each row, I want to find the Q5(the 
> > 5th quantile) and the Q95, and put them in a function fyn(), which 
> > will return two numbers n and m(n<m). Then I'll take each element (say 
> > x) in the row one by one, and perform a judgment: if x<n, do sub1(x); 
> > if n<=x<=m, do sub2(x); if x>m, do sub3(x). The process goes on 
> > through each row in the matrix. This program isnot very complicated, 
> > but hard enough for me...........So could anybody give me a prototype 
> > of the code?
> > Thanks a lot!
> >
> > _______________________________________________
> > Bioconductor mailing list
> > Bioconductor at stat.math.ethz.ch
> > https://stat.ethz.ch/mailman/listinfo/bioconductor
> 
> _______________________________________________
> Bioconductor mailing list
> Bioconductor at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/bioconductor
>



More information about the Bioconductor mailing list