使用 knitr 包的相关矩阵
Correaltion matrix using knitr package
是否可以使用 knitr() 或类似方法创建如下图所示的相关矩阵?
为
res <- cor(iris[,1:4])
scipub
包有一个函数,correltable
,可以生成具有重要星号的相关表。您需要安装 htmlTable
包才能在 HTML.
中获取这些相关表
是你想要的吗?
---
title: "Correlation table"
author: "Stéphane Laurent"
date: "17/01/2022"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(scipub)
library(mvtnorm)
set.seed(666L)
```
```{r}
dat <- as.data.frame(rmvnorm(50, sigma = toeplitz(3:1)))
colnames(dat) <- c("V1", "V2", "V3")
```
```{r, results='asis'}
correltable(dat, html = TRUE)
```
编辑
添加均值和标准差:
---
title: "Correlation table"
author: "Stéphane Laurent"
date: "17/01/2022"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(scipub)
library(htmlTable)
library(magrittr)
library(mvtnorm)
set.seed(666L)
```
```{r}
dat <- as.data.frame(rmvnorm(50, sigma = toeplitz(3:1)))
colnames(dat) <- c("V1", "V2", "V3")
cortable <- correltable(dat, html = FALSE)
table2 <- as.data.frame(matrix(as.character(cortable$table), nrow = 3))
caption <- cortable[["caption"]]
Means <- formatC(colMeans(dat))
Sds <- formatC(apply(dat, 2L, sd))
table1 <- data.frame(Mean = Means, SD = Sds)
```
```{r, results='asis'}
css.cell <- matrix("padding: 5px;", ncol = 6L, nrow = 4L)
css.cell[, 1L] <-
paste(css.cell[, 1L], "font-weight: bold;") # <-- bold row names
cbind(table1, table2) %>%
addHtmlTableStyle(css.cell = css.cell) %>%
htmlTable(caption = caption)
```
是否可以使用 knitr() 或类似方法创建如下图所示的相关矩阵?
为
res <- cor(iris[,1:4])
scipub
包有一个函数,correltable
,可以生成具有重要星号的相关表。您需要安装 htmlTable
包才能在 HTML.
是你想要的吗?
---
title: "Correlation table"
author: "Stéphane Laurent"
date: "17/01/2022"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(scipub)
library(mvtnorm)
set.seed(666L)
```
```{r}
dat <- as.data.frame(rmvnorm(50, sigma = toeplitz(3:1)))
colnames(dat) <- c("V1", "V2", "V3")
```
```{r, results='asis'}
correltable(dat, html = TRUE)
```
编辑
添加均值和标准差:
---
title: "Correlation table"
author: "Stéphane Laurent"
date: "17/01/2022"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(scipub)
library(htmlTable)
library(magrittr)
library(mvtnorm)
set.seed(666L)
```
```{r}
dat <- as.data.frame(rmvnorm(50, sigma = toeplitz(3:1)))
colnames(dat) <- c("V1", "V2", "V3")
cortable <- correltable(dat, html = FALSE)
table2 <- as.data.frame(matrix(as.character(cortable$table), nrow = 3))
caption <- cortable[["caption"]]
Means <- formatC(colMeans(dat))
Sds <- formatC(apply(dat, 2L, sd))
table1 <- data.frame(Mean = Means, SD = Sds)
```
```{r, results='asis'}
css.cell <- matrix("padding: 5px;", ncol = 6L, nrow = 4L)
css.cell[, 1L] <-
paste(css.cell[, 1L], "font-weight: bold;") # <-- bold row names
cbind(table1, table2) %>%
addHtmlTableStyle(css.cell = css.cell) %>%
htmlTable(caption = caption)
```