如何通过w/corstarsl()函数实现干净的关联表输出? [英] How to to achieve clean correlation table output w/ corstarsl() function?
问题描述
有一个很好的关联表功能周围(我所知道的唯一一种可以使人产生这种特殊的熟悉感). Bertold 已经修改了代码,以实现更好的输出.但是,有一些情况. G.当输出看起来仍然有些混乱时,具有负相关性.
There's a nice correlation table function around (the only one I know producing this special kind of familiarity to ones eye). Bertold already has modified the code in order to achieve a nicer output. However, there are situations e. g. w/ negative correlations when the output still looks somewhat messed up.
在下面,我首先在一个最小示例下面显示该函数:
In the following I show the function first and below a minimal example:
corstarsl <- function(x){
# corstars1() computes a correlation matrix w/ significance stars
require(Hmisc)
x <- as.matrix(x)
R <- rcorr(x)$r
p <- rcorr(x)$P
## define significance levels
mystars <- ifelse(p < .001, "***",
ifelse(p < .01, "** ",
ifelse(p < .05, "* ", " ")))
## correlation matrix w/ two digits
R <- format(round(cbind(rep(- 1.11, ncol(x)), R), 2))[, -1]
## build a new matrix that includes the correlations w/ appropriate stars
Rnew <- matrix(paste(R, mystars, sep = ""), ncol = ncol(x))
diag(Rnew) <- paste(diag(R), " ", sep = "")
rownames(Rnew) <- colnames(x)
colnames(Rnew) <- paste(colnames(x), "", sep = "")
## remove upper triangle
Rnew <- as.matrix(Rnew)
Rnew[upper.tri(Rnew, diag = TRUE)] <- ""
Rnew <- as.data.frame(Rnew)
## remove last column and return the matrix (which is now a data frame)
Rnew <- cbind(Rnew[1:length(Rnew) - 1])
return(Rnew)
}
示例:
library(MASS)
n <- 100
mymeans <- c(10, 12, 15, 17) # means of each var
Sigma <- matrix(c(1, -.45, .16, -.71,
-.45, 1, -.71, .09,
.16, -.71, 1, -.17,
-.71, .09, -.17, 1), ncol = 4)
dat <- mvrnorm(n = n, mu = mymeans, Sigma, empirical = TRUE)
(cortab <- corstarsl(as.data.frame(dat)))
## V1 V2 V3
## V1
## V2 -0.45***
## V3 0.16 -0.71***
## V4 -0.71*** 0.09 -0.17
# or with htmlTable():
library(htmlTable)
htmlTable(cortab,
align = paste(rep('l', ncol(cortab)), collapse = ''))
输出中断:
编辑:通过左对齐,我快到了,但是如何给正值更多的空间?
Edit: With the left-align I'm almost there, but how to give the positive values more space?
预期输出:
有人知道如何在函数内更好地格式化结果(小数点应对齐)?
Does anyone know how to achieve a better formatting of the result inside the function (decimal points should be aligned)?
推荐答案
您可以使用tableHTML
并使用widths
参数来控制列的宽度.另外,您需要使用
,它是html字符,例如:
You can use tableHTML
and use the widths
argument to control the widths of columns. Also, you would need to use
which is the html character for a space like this:
#convert to character
cortab[] <- lapply(cortab, as.character)
#if the cell does not start with a minus add an html space
cortab[] <- lapply(cortab, function(x) {
ifelse(!startsWith(x, '-'), paste0(' ', x), x)
})
#convert to html with tableHTML
#control the column widths with the widths argument
library(tableHTML)
tableHTML(cortab, widths = c(40, 60, 60, 60), theme = 'scientific') %>%
add_css_column(list('text-align', 'left !important'), columns = 1:4)
这篇关于如何通过w/corstarsl()函数实现干净的关联表输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!