Can someone please give me a pointer here. I have two matrices matA A B C 1 5 2 4 2 2 4 3 3 1 2 4 matB A B C 1 TRUE FALSE TRUE 2 FALSE TRUE TRUE 3 FALSE FALSE FALSE how do I extract all the values from matA where the coresponding entry in matB == TRUE (or FALSE), perferably in vector form. Many thanks tom
Thanks everyone. Obvious when you think about it, and you check that both the matrices your trying it with are actually matrices... instead of one being a list. On Wed, 2006-15-03 at 06:03 -0500, tom wright wrote:> Can someone please give me a pointer here. > I have two matrices > > matA > A B C > 1 5 2 4 > 2 2 4 3 > 3 1 2 4 > > matB > A B C > 1 TRUE FALSE TRUE > 2 FALSE TRUE TRUE > 3 FALSE FALSE FALSE > > how do I extract all the values from matA where the coresponding entry > in matB == TRUE (or FALSE), perferably in vector form. > > Many thanks > tom > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Try this: matA[c(matB)] In fact even this works for your example although in general it couldbe problematic since a two column matrix index has special meaning: matA[matB] On 3/15/06, tom wright <tom at maladmin.com> wrote:> Can someone please give me a pointer here. > I have two matrices > > matA > A B C > 1 5 2 4 > 2 2 4 3 > 3 1 2 4 > > matB > A B C > 1 TRUE FALSE TRUE > 2 FALSE TRUE TRUE > 3 FALSE FALSE FALSE > > how do I extract all the values from matA where the coresponding entry > in matB == TRUE (or FALSE), perferably in vector form. > > Many thanks > tom > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
On Wed, 2006-03-15 at 06:03 -0500, tom wright wrote:> Can someone please give me a pointer here. > I have two matrices > > matA > A B C > 1 5 2 4 > 2 2 4 3 > 3 1 2 4 > > matB > A B C > 1 TRUE FALSE TRUE > 2 FALSE TRUE TRUE > 3 FALSE FALSE FALSE > > how do I extract all the values from matA where the coresponding entry > in matB == TRUE (or FALSE), perferably in vector form. > > Many thanks > tomThe subsetting/indexing is premised on the index values being TRUE, thus:> matA[matB][1] 5 4 4 3> matA[!matB][1] 2 1 2 2 4 HTH, Marc Schwartz
This is really elementary indexing in S language: matA[matB] Best, Philippe Grosjean tom wright wrote:> Can someone please give me a pointer here. > I have two matrices > > matA > A B C > 1 5 2 4 > 2 2 4 3 > 3 1 2 4 > > matB > A B C > 1 TRUE FALSE TRUE > 2 FALSE TRUE TRUE > 3 FALSE FALSE FALSE > > how do I extract all the values from matA where the coresponding entry > in matB == TRUE (or FALSE), perferably in vector form. > > Many thanks > tom > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >
> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of tom wright > Sent: Wednesday, March 15, 2006 6:04 AM > To: R-Stat Help > Subject: [R] matrix indexing > > > Can someone please give me a pointer here. > I have two matrices > > matA > A B C > 1 5 2 4 > 2 2 4 3 > 3 1 2 4 > > matB > A B C > 1 TRUE FALSE TRUE > 2 FALSE TRUE TRUE > 3 FALSE FALSE FALSE > > how do I extract all the values from matA where the coresponding entry > in matB == TRUE (or FALSE), perferably in vector form.Lie this? matA <- matrix(c(5,2,1,2,4,2,4,3,4),3,3) matB <- matrix(c(T,F,F,F,T,F,T,T,F),3,3) matA[matB] matA[!matB]> > Many thanks > tom > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
Dear tom, is the following what you are looking for? > a=matrix(runif(9),3,3) > a [,1] [,2] [,3] [1,] 0.9484247 0.9765431 0.6169739 [2,] 0.8423545 0.3137295 0.4031847 [3,] 0.6724235 0.1076373 0.2356923 > b<-matrix(sample(c(TRUE,FALSE),size=9,replace=TRUE),3,3) > b [,1] [,2] [,3] [1,] FALSE TRUE TRUE [2,] TRUE TRUE TRUE [3,] FALSE FALSE FALSE > a[b] [1] 0.8423545 0.9765431 0.3137295 0.6169739 0.4031847 > best, vito tom wright wrote:> Can someone please give me a pointer here. > I have two matrices > > matA > A B C > 1 5 2 4 > 2 2 4 3 > 3 1 2 4 > > matB > A B C > 1 TRUE FALSE TRUE > 2 FALSE TRUE TRUE > 3 FALSE FALSE FALSE > > how do I extract all the values from matA where the coresponding entry > in matB == TRUE (or FALSE), perferably in vector form. > > Many thanks > tom > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- ===================================Vito M.R. Muggeo Dip.to Sc Statist e Matem `Vianelli' Universit? di Palermo viale delle Scienze, edificio 13 90128 Palermo - ITALY tel: 091 6626240 fax: 091 485726/485612
matA[matB] or matA[!matB] 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://www.med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "tom wright" <tom at maladmin.com> To: "R-Stat Help" <R-help at stat.math.ethz.ch> Sent: Wednesday, March 15, 2006 12:03 PM Subject: [R] matrix indexing> Can someone please give me a pointer here. > I have two matrices > > matA > A B C > 1 5 2 4 > 2 2 4 3 > 3 1 2 4 > > matB > A B C > 1 TRUE FALSE TRUE > 2 FALSE TRUE TRUE > 3 FALSE FALSE FALSE > > how do I extract all the values from matA where the coresponding > entry > in matB == TRUE (or FALSE), perferably in vector form. > > Many thanks > tom > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Possibly Parallel Threads
- In which column and in which row a number is in a matrix
- comparing matrices
- overlapping graphs in logarithmic y-axis
- how to apply the dummy coding rule in a dataframe with complete factor levels to another dataframe with incomplete factor levels?
- save txt file