asking for some code
2
0
Entering edit mode
zhihua li ▴ 120
@zhihua-li-1129
Last seen 10.2 years ago
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&lt;=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!
PROcess PROcess • 1.5k views
ADD COMMENT
0
Entering edit mode
@sean-davis-490
Last seen 3 months ago
United States
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@stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/bioconductor
ADD COMMENT
0
Entering edit mode
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@stat.math.ethz.ch > > https://stat.ethz.ch/mailman/listinfo/bioconductor > > _______________________________________________ > Bioconductor mailing list > Bioconductor@stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/bioconductor >
ADD REPLY
0
Entering edit mode
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
ADD REPLY
0
Entering edit mode
On Wed, Mar 09, 2005 at 06:06:26PM +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") > Sorry, I was a bit quick: that stuff only works when x is a character variable.
ADD REPLY
0
Entering edit mode
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 >
ADD REPLY
0
Entering edit mode
Thanks for all the help! I'm on the border of resolving the problem but......... for the if-else clause, I alwayse got error message when using if..else if....else.....statement. I looked it up in the R mannual and found that R only have a "if....else..." structure, but doesn't have something like "if....elseif....elseif......else....."? Does it mean that only two conditions can be selected in the conditional execution grammer of R? for the cut() and switch() clause, I found it's very efficient in application to a vector. but when I wanna use it in a matrix row by row, it's less convenient (or I didn't write the smart code). >From: Adaikalavan Ramasamy <ramasamy@cancer.org.uk> >Reply-To: ramasamy@cancer.org.uk >To: Sean Davis <sdavis2@mail.nih.gov> >CC: zhihua li <lzhtom@hotmail.com>, BioConductor mailing list <bioconductor@stat.math.ethz.ch> >Subject: Re: [BioC] asking for some code >Date: Wed, 09 Mar 2005 14:41:36 +0000 > >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@stat.math.ethz.ch > > > https://stat.ethz.ch/mailman/listinfo/bioconductor > > > > _______________________________________________ > > Bioconductor mailing list > > Bioconductor@stat.math.ethz.ch > > https://stat.ethz.ch/mailman/listinfo/bioconductor > > >
ADD REPLY
0
Entering edit mode
zhihua li ▴ 120
@zhihua-li-1129
Last seen 10.2 years ago
Ya, it works! maybe I made some mistakes writing the if...else clause yesterday. Thanks! >From: "STKH (Steen Krogsgaard)" <stkh@novozymes.com> >To: "zhihua li" <lzhtom@hotmail.com> >Subject: RE: [BioC] asking for some code >Date: Thu, 10 Mar 2005 14:20:28 +0100 > >Is there anything wrong with > >if (clause1) { >... >} else if (clause2) { >... >} else if (clause3) { >... >} else { >... >} > >That's what you do in Pascal! > >/Steen > >-----Original Message----- >From: bioconductor-bounces@stat.math.ethz.ch >[mailto:bioconductor-bounces@stat.math.ethz.ch] On Behalf Of zhihua li >Sent: 10. marts 2005 14:12 >To: ramasamy@cancer.org.uk >Cc: bioconductor@stat.math.ethz.ch >Subject: Re: [BioC] asking for some code > > >Thanks for all the help! I'm on the border of resolving the problem >but......... > >for the if-else clause, I alwayse got error message when using if..else >if....else.....statement. I looked it up in the R mannual and found that >R only have a "if....else..." structure, but doesn't have something like > >"if....elseif....elseif......else....."? Does it mean that only two >conditions can be selected in the conditional execution grammer of R? > >for the cut() and switch() clause, I found it's very efficient in >application to a vector. >but when I wanna use it in a matrix row by row, it's less convenient (or >I >didn't write the >smart code). > > > > >From: Adaikalavan Ramasamy <ramasamy@cancer.org.uk> > >Reply-To: ramasamy@cancer.org.uk > >To: Sean Davis <sdavis2@mail.nih.gov> > >CC: zhihua li <lzhtom@hotmail.com>, BioConductor mailing list ><bioconductor@stat.math.ethz.ch> > >Subject: Re: [BioC] asking for some code > >Date: Wed, 09 Mar 2005 14:41:36 +0000 > > > >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@stat.math.ethz.ch > > > > https://stat.ethz.ch/mailman/listinfo/bioconductor > > > > > > _______________________________________________ > > > Bioconductor mailing list > > > Bioconductor@stat.math.ethz.ch > > > https://stat.ethz.ch/mailman/listinfo/bioconductor > > > > > > >_______________________________________________ >Bioconductor mailing list >Bioconductor@stat.math.ethz.ch >https://stat.ethz.ch/mailman/listinfo/bioconductor
ADD COMMENT

Login before adding your answer.

Traffic: 941 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6