Skip to content
Prev 277771 / 398506 Next

return only pairwise correlations greater than given value

This is probably not the prettiest or most efficient function ever, but this
seems to do what I wanted.


spec.cor <- function(dat, r, ...){
	
	require("reshape")
	
	d1 <- data.frame(cor(dat))
	d2 <- melt(d1)
	d2[,3] <- rep(rownames(d1), nrow(d2)/length(unique(d2[,1])))
	d2 <- d2[,c("variable", "V3", "value")]
	colnames(d2) <- c("V1", "V2", "value")
	d2 <- d2[with(d2, which(V1 != V2, arr.ind=T)), ]
	d2 <- d2[which(d2[,3] >=r | d2[,3] <= -r, arr.ind=T),]
	d2[,1:2] <- t(apply(d2[,1:2], MARGIN=1, function(x) sort(x)))
	d2 <- unique(d2)	
	
	return(d2)
}



data(mtcars)
Using  as id variables
    V1   V2      value
2  cyl disp  0.9020329
3  cyl   hp  0.8324475
4  cyl drat -0.6999381
7 disp   hp  0.7909486
8 disp drat -0.7102139



I'm not sure how to make melt() quit giving the "Using  as id variables"
warning, but I don't really care either.
B77S wrote:
--
View this message in context: http://r.789695.n4.nabble.com/return-only-pairwise-correlations-greater-than-given-value-tp4079028p4081534.html
Sent from the R help mailing list archive at Nabble.com.