[R] return only pairwise correlations greater than given value

Joshua Wiley jwiley.psych at gmail.com
Fri Nov 18 00:33:31 CET 2011


Hi Brad,

You do not really need to reshape the correlation matrix.  This seems
to do what you want:

spec.cor <- function(dat, r, ...) {
  x <- cor(dat, ...)
  x[upper.tri(x, TRUE)] <- NA
  i <- which(abs(x) >= r, arr.ind = TRUE)
  data.frame(matrix(colnames(x)[as.vector(i)], ncol = 2), value = x[i])
}

spec.cor(mtcars[, 2:5], .6)

Cheers,

Josh

On Wed, Nov 16, 2011 at 9:58 PM, B77S <bps0002 at auburn.edu> wrote:
> Thanks Michael,
>
> I just started on the following code (below), and realized I should as as
> this might exist.
>
> basically what I'd like is for the function to return (basically) what you
> just suggested, plus the names of the two variables (I suppose pasted
> together would be good).
>
> I hope that is clear.
>
> #
> sig.cor <- function(dat, r, ...){
>
> cv2 <- data.frame(cor(dat))
> var.names <- rownames(cv2)
>
> list.cv2 <- which(cv2 >=r | cv2 <= -r, arr.ind=T)
> cor.r <- cv2[list.cv2[which(list.cv2 [,"row"]!=list.cv2 [,"col"]),]]
> cor.names <- var.names[list.cv2[which(list.cv2 [,"row"]!=list.cv2
> [,"col"]),]]
>
>
> return(cor.r)
>
> }
>
>
>
>
>
> Michael Weylandt wrote:
>>
>> What exactly do you mean "returns" them? More generally I suppose,
>> what do you have in mind to do with this?
>>
>> You could do something like this:
>>
>> BigCorrelation <- function(X){
>>
>>      return(which(abs(cor(X)) > 0.9, arr.ind = T))
>> }
>>
>> but it hardly seems worth its own function call.
>>
>> On Thu, Nov 17, 2011 at 12:42 AM, B77S <bps0002@> wrote:
>>> Hello,
>>>
>>>  I would like to find out if a function already exists that returns only
>>> pairwise correlations above/below a certain threshold (e.g, -.90, .90)
>>>
>>> Thank you.
>>>
>>>
>>>
>>> --
>>> View this message in context:
>>> http://r.789695.n4.nabble.com/return-only-pairwise-correlations-greater-than-given-value-tp4079028p4079028.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> R-help@ 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.
>>>
>>
>> ______________________________________________
>> R-help@ 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.
>>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/return-only-pairwise-correlations-greater-than-given-value-tp4079028p4079044.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, ATS Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list