Rilcon42
Rilcon42

Reputation: 9765

listing p-values with correlations

I successfully got an ordered list of correlation values from a matrix based on this question. I would like to include p-values like this:

  Var1 Var2  Freq  p-val
6 col3 col2  0.60  0.06
2 col2 col1 -0.38  0.28
3 col3 col1 -0.72  0.01

I changed the code to use corr.test so I could get p-values, but im not sure how to put them in the list.

require(psych)
set.seed(1)
df <- data.frame(
    col1 = rnorm(10),
    col2 = rnorm(10),
    col3=rnorm(10)
)

cors<-corr.test(df)
listCorr<-as.data.frame.table(cors$r)[lower.tri(cors$r,diag=FALSE),]
listCorr$Freq<-signif(listCorr$Freq,2)
listCorr<-listCorr[with(listCorr,order(-listCorr[,3])),]

Upvotes: 2

Views: 60

Answers (1)

mts
mts

Reputation: 2190

library(psych)

set.seed(1)
df <- data.frame(
  col1 = rnorm(10),
  col2 = rnorm(10),
  col3=rnorm(10)
)

cors<-corr.test(df)
listCorr<-as.data.frame.table(cors$r)[lower.tri(cors$r,diag=FALSE),]
listCorr$Freq<-signif(listCorr$Freq,2)
listCorr$p.val <- signif((cors$p)[lower.tri(cors$p)], 2)
listCorr<-listCorr[with(listCorr,order(-listCorr[,3])),]

> listCorr
  Var1 Var2  Freq p.val
6 col3 col2  0.60 0.064
2 col2 col1 -0.38 0.280
3 col3 col1 -0.72 0.020

Upvotes: 1

Related Questions