[R] Filtering matrices

Peter Alspach Peter.Alspach at plantandfood.co.nz
Wed Aug 26 06:39:34 CEST 2009


Tena koe Ben 

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of bwgoudey
> Sent: Wednesday, 26 August 2009 2:55 p.m.
> To: r-help at r-project.org
> Subject: Re: [R] Filtering matrices
> 
> 
> > 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
> 
> 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?
> 
> Cheers
>  Ben

It is not clear to me what 'matrix' you wish to have returned since
you'll be keeping an unknown number of values.  However, if you are
happy just to replace the unwanted values with a missing value then
something like

r[[1]][r[[1]]<0.75 | (r[[3]]>0.05 & !is.na(r[[3]]))] <- NA

might work.  I say 'might' because I am unfamiliar with rcorr() and thus
am not sure whether the components of r are matrices or dataframes, and
the above is untested.  You might be interested in abs(r[[1]]).

HTH ....

Peter Alspach




More information about the R-help mailing list