# [R] Filtering matrices

Steve Lianoglou mailinglist.honeypot at gmail.com
Wed Aug 26 15:38:28 CEST 2009

```Hi,

On Aug 25, 2009, at 10:54 PM, bwgoudey wrote:

>
>> r<-rcorr(d[[1]]) #d is matrix containing observation
>> r[[1]] #r values
>           age         sex         BMI
> age  1.0000000 -0.30010322 -0.13702263
> sex -0.3001032  1.00000000  0.06300528
> BMI -0.1370226  0.06300528  1.00000000
>> r[[2]] #Number of obervations
>    age sex BMI
> age 100 100 100
> sex 100 100 100
> BMI 100 100 100
>> r[[3]] #P values
>            age         sex       BMI
> age          NA 0.002416954 0.1740134
> sex 0.002416954          NA 0.5334484
> BMI 0.174013354 0.533448366        NA

Just a quick note: please provide data in an easy way for us to enter
into our R session -- the way you provide the data requires more work
on someone who is trying to help you in order to enter it in R, for
instance this might have been better:

rvals <- matrix(c(
1.0000000, -0.30010322, -0.13702263,
-0.3001032,  1.00000000,  0.06300528,
-0.1370226,  0.06300528,  1.00000000), byrow=TRUE, nrow=3)

obs <- matrix(c(
100, 100, 100,
100, 100, 100,
100, 100, 100), byrow=TRUE, nrow=3)

pval <- matrix(c(
NA, 0.002416954, 0.1740134,
0.002416954,          NA, 0.5334484,
0.174013354, 0.533448366,        NA), byrow=TRUE, nrow=3)

Since I can just copy and paste that into my R session and get right

> If I wanted to return a matrix containing all points where
> correlation was
> above 0.75 and P-value was below 0.05, how would I do this?

Where is your "points" matrix that you want to return the data from?

Assuming this is also a 3x3 matrix, you just build the indexing
vectors using the data matrices of interest, and use those to pull the
points out of your data/"points" matrix.

Let's assume my data/points matrix is called my.data:

#1 Get indices of points w/ correlation above 0.75
good.cor <- rvals > .75

#2 Get indices of points w/ p-value < 0.05, since you have NA values
# in your pval matrix, you have to *explicitly exclude* them from the
query
good.p   <- !is.na(pval) & pval < 0.05

# since you want their intersection, take an & of the two vectors,
# but this is empty in this case. Either way, this would get
# the points you're after
my.data[good.cor & good.p]

Does that make sense?

-steve

--
Steve Lianoglou