Hi, Given a simple example, test <- matrix(c(0.1, 0.2, 0.1, 0.2, 0.1, 0.1, 0.3, 0.1, 0.1), 3, 3) How to generate row indexes for which their corresponding row values are less than or equal to 0.2 ? For this example, row 2 and 3 are the correct ones. Thanks [[alternative HTML version deleted]]
which(apply(test<=0.2, 1, all)) See ?which, ?all, and in particular ?apply. Gabor On Mon, Feb 11, 2008 at 06:22:09PM +0800, Ng Stanley wrote:> Hi, > > Given a simple example, test <- matrix(c(0.1, 0.2, 0.1, 0.2, 0.1, 0.1, 0.3, > 0.1, 0.1), 3, 3) > > How to generate row indexes for which their corresponding row values are > less than or equal to 0.2 ? For this example, row 2 and 3 are the correct > ones. > > Thanks > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.-- Csardi Gabor <csardi at rmki.kfki.hu> UNIL DGM
do you mean the following: test <- matrix(c(0.1, 0.2, 0.1, 0.2, 0.1, 0.1, 0.3, 0.1, 0.1), 3, 3) ind <- rowSums(test <= 0.2) == nrow(test) ind which(ind) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Ng Stanley" <stanleyngkl at gmail.com> To: "r-help" <r-help at r-project.org> Sent: Monday, February 11, 2008 11:22 AM Subject: [R] Conditional rows> Hi, > > Given a simple example, test <- matrix(c(0.1, 0.2, 0.1, 0.2, 0.1, > 0.1, 0.3, > 0.1, 0.1), 3, 3) > > How to generate row indexes for which their corresponding row values > are > less than or equal to 0.2 ? For this example, row 2 and 3 are the > correct > ones. > > Thanks > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
That works beautfully. Why using test<=0.2 || test >0.3 gives error ? -----Original Message----- From: Gabor Csardi [mailto:csardi at rmki.kfki.hu] Sent: Monday, February 11, 2008 18:27 To: Ng Stanley Cc: r-help Subject: Re: [R] Conditional rows which(apply(test<=0.2, 1, all)) See ?which, ?all, and in particular ?apply. Gabor On Mon, Feb 11, 2008 at 06:22:09PM +0800, Ng Stanley wrote:> Hi, > > Given a simple example, test <- matrix(c(0.1, 0.2, 0.1, 0.2, 0.1, > 0.1, 0.3, 0.1, 0.1), 3, 3) > > How to generate row indexes for which their corresponding row values > are less than or equal to 0.2 ? For this example, row 2 and 3 are the > correct ones. > > Thanks > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.-- Csardi Gabor <csardi at rmki.kfki.hu> UNIL DGM
Because you need test <= 0.2 | test >0.3 See ?"|" Gabor On Mon, Feb 11, 2008 at 09:12:57PM +0800, Stanley Ng wrote:> That works beautfully. Why using test<=0.2 || test >0.3 gives error ? > > -----Original Message----- > From: Gabor Csardi [mailto:csardi at rmki.kfki.hu] > Sent: Monday, February 11, 2008 18:27 > To: Ng Stanley > Cc: r-help > Subject: Re: [R] Conditional rows > > which(apply(test<=0.2, 1, all)) > > See ?which, ?all, and in particular ?apply. > > Gabor > > On Mon, Feb 11, 2008 at 06:22:09PM +0800, Ng Stanley wrote: > > Hi, > > > > Given a simple example, test <- matrix(c(0.1, 0.2, 0.1, 0.2, 0.1, > > 0.1, 0.3, 0.1, 0.1), 3, 3) > > > > How to generate row indexes for which their corresponding row values > > are less than or equal to 0.2 ? For this example, row 2 and 3 are the > > correct ones. > > > > Thanks[...] -- Csardi Gabor <csardi at rmki.kfki.hu> UNIL DGM