Skip to content

Instantly share code, notes, and snippets.

@smithdanielle
Created April 17, 2014 16:34
Show Gist options
  • Save smithdanielle/10996464 to your computer and use it in GitHub Desktop.
Save smithdanielle/10996464 to your computer and use it in GitHub Desktop.
A function to output a correlation matrix with p-values and significance indicators in `R`.
corstars <- function(x){
require(Hmisc)
x <- as.matrix(x)
R <- rcorr(x)$r
p <- rcorr(x)$P
mystars <- ifelse(p < .01, "**|", ifelse(p < .05, "* |", " |"))
R <- format(round(cbind(rep(-1.111, ncol(x)), R), 3))[,-1]
Rnew <- matrix(paste(R, mystars, sep=""), ncol=ncol(x))
diag(Rnew) <- paste(diag(R), " |", sep="")
rownames(Rnew) <- colnames(x)
colnames(Rnew) <- paste(colnames(x), "|", sep="")
Rnew <- as.data.frame(Rnew)
return(Rnew)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment