correltable | R Documentation |
The correltable
function can be used to create correlation
table (with stars for significance) for scientific publication
This is intended to summarize correlations between (vars
)
from an input dataset (data
).
Correlations are based on stats::cor
, use
and method
follow from that function.
Stars indicate significance: *p<.05, **p<.01, ***p<.001
For formatting, variables can be renamed, numbers can be rounded,
upper or lower triangle only can be selected (or whole matrix),
and empty columns/rows can be dropped if using triangles.
For more compact columns, variable names can be numbered in the
rows and column names will be corresponding numbers.
If only cross-correlation between two sets of variables is desired
(no correlations within a set of variables),
vars2
and var_names
can be used.
This function will drop any non-numeric variables by default.
Requires tidyverse
and stats
libraries.
correltable(
data,
vars = NULL,
var_names = vars,
vars2 = NULL,
var_names2 = vars2,
method = c("pearson", "spearman"),
use = c("pairwise", "complete"),
round_n = 2,
tri = c("upper", "lower", "all"),
cutempty = c(FALSE, TRUE),
colnum = c(FALSE, TRUE),
html = c(FALSE, TRUE),
strata = NULL
)
data |
The input dataset. |
vars |
A list of the names of variables to correlate,
e.g. c("Age","height","WASI"),
if NULL, all variables in |
var_names |
An optional list to rename the |
vars2 |
If cross-correlation between two sets of variables
is desired, add a second list of variables to correlate with
|
var_names2 |
An optional list to rename the |
method |
Type of correlation to calculate c("pearson", "spearman"),
based on |
use |
Use pairwise.complete.obs or restrict to complete cases
c("pairwise", "complete"), based on |
round_n |
The number of decimal places to round all output to (default=2). |
tri |
Select output formatting c("upper", "lower","all"); KEEP the upper triangle, lower triangle, or all values, default ="upper. |
cutempty |
If keeping only upper/lower triangle with |
colnum |
For more concise column names, number row names and just use corresponding numbers as column names, default=FALSE, if TRUE overrides cutempty. |
html |
Format as html in viewer or not (default=F, print in console), needs library(htmlTable) installed. |
strata |
Split table by a 2-level factor variable with level1 in the upper and level2 in the lower triangle must have 2+ cases per level, cannot be combined with vars2 |
Output Table 1
correltable(data = psydat)
correltable(
data = psydat, vars = c("Age", "Height", "iq"),
tri = "lower", html = TRUE
)
correltable(
data = psydat, vars = c("Age", "Height", "iq"),
tri = "lower", html = TRUE, strata = "Sex"
)
correltable(
data = psydat, vars = c("Age", "Height", "iq"),
var_names = c("Age (months)", "Height (inches)", "IQ"),
tri = "upper", colnum = TRUE, html = TRUE
)
correltable(
data = psydat, vars = c("Age", "Height", "iq"),
var_names = c("Age (months)", "Height (inches)", "IQ"),
vars2 = c("depressT", "anxT"),
var_names2 = c("Depression T", "Anxiety T"), html = TRUE
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.