Enumerating 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:

  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

to get the p-values, but I'm 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])),]

      

+3


source to share


1 answer


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

      



+1


source







All Articles