matrix indexing

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

matrix indexing

Tom Wright-9
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: matrix indexing

Tom Wright-9
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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: matrix indexing

Gabor Grothendieck
In reply to this post by Tom Wright-9
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 <[hidden email]> 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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: matrix indexing

Marc Schwartz (via MN)
In reply to this post by Tom Wright-9
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
> tom

The 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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: matrix indexing

Philippe Grosjean
In reply to this post by Tom Wright-9
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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: matrix indexing

Andy Bunn
In reply to this post by Tom Wright-9
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]]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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: matrix indexing

vito muggeo
In reply to this post by Tom Wright-9
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
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: matrix indexing

Dimitris Rizopoulos
In reply to this post by Tom Wright-9
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" <[hidden email]>
To: "R-Stat Help" <[hidden email]>
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
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html