我想将R中的互变关联表转换为根据APA样式格式化的表。该表应具有以下特征
答案 0 :(得分:0)
我使用Hmisc
创建了与p-values
的关联表,但是如果没有包,这是可行的,特别是如果你想要Kendall的tau相关性。
#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(-)