r - Appending p-values to correlation matrix dataframe -
i trying create correlation matrix p-value right next correlation, not sure how merge two.
my code:
library(psych) set.seed(1) df <- data.frame( col1 = rnorm(10), col2 = rnorm(10), col3=rnorm(10) ) cors<-corr.test(df) outputdf<-as.data.frame(cors$r)[lower.tri(cors$r,diag=false),] p.val <- (cors$p)[lower.tri(cors$p)] view(outputdf)
my goal (p-values may incorrect due r issue on end):
col1 col2 col2 -0.37(0.283) col3 -0.71(0.019) 0.60(0.06)
we use paste
paste corresponding elements in 'cors$r' , 'cors$p'. using []
same structure cors$p
. change elements in diagonal , upper triangular matrix ''
, subset rows , columns have @ least 1 non-empty element.
res <- cors$p res[] <- paste0(round(cors$r,2), "(", round(cors$p,2), ")") res[upper.tri(res, diag=true)] <- '' res[!!rowsums(res!=''),!!colsums(res!='')] # col1 col2 #col2 "-0.38(0.28)" "" #col3 "-0.72(0.02)" "0.6(0.06)"
Comments
Post a Comment