I used Hmisc
to create correlation table with p-values
but this is doable without the package, especially if you want Kendall's tau correlations.
#First indicate which columns do you want in your correlation table
cor_cols = c('gender','age','education','expense',
'learningmotivation', 'reading')
#Slice the data and calculate correlation table:
mt = as.matrix(data[,cor_cols])
library(Hmisc)
myrcorr <- rcorr(mt, type="spearman")
#format numbers
cor_table <-as.matrix( round(myrcorr$r,2))
#remove rownames and colnames
colnames(cor_table)<- rownames(cor_table)<- NULL
#remove leading zeros and add stars
n <- length(cor_cols)
for (c in 1:n) {
for (r in 1:n) {
pval <- myrcorr$P[r,c]
stars <- ifelse(pval < .001, "***", ifelse(pval < .01, "** ", ifelse(pval < .05, "* ", " ")))
coeff = sub("0*\\.",".",cor_table[r,c]) #remove leading zeros
cor_table[r,c] = paste(c(coeff,stars),collapse='')
}
}
cor_table[upper.tri(cor_table)] <- '' # erase the upper triangle
diag(cor_table) <- '-' # replace the diagonals by dash(-)