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
)

Arguments

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 data will be used.

var_names

An optional list to rename the vars colnames in the output table, e.g. c("Age (years)","Height (inches)","IQ"). Must match vars in length. If not supplied, vars will be printed as is.

vars2

If cross-correlation between two sets of variables is desired, add a second list of variables to correlate with vars; Overrides tri, cutempty, and colnum.

var_names2

An optional list to rename the vars2 colnames in the output table If not supplied, vars2 will be printed as is.

method

Type of correlation to calculate c("pearson", "spearman"), based on stats::cor, default = "pearson".

use

Use pairwise.complete.obs or restrict to complete cases c("pairwise", "complete"), based on stats::cor, default = "pairwise".

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 tri, cut empty row/column, default=FALSE.

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

Value

Output Table 1

Examples

correltable(data = psydat)
#> Warning: Converting non-numeric columns to factor: Sex,Income
#> $table #> Age Sex Income Height iq depressT anxT #> Age t=-1.86 F=4.17* .41*** .09*** .02 -.01 #> Sex \u03c72=0.72 t=0.83 t=1.25 t=-4.87*** t=-5.76*** #> Income F=1.15 F=364.33*** F=31.18*** F=16.26*** #> Height .04* -.01 -.01 #> iq -.08*** -.06*** #> depressT .61*** #> anxT #> #> $caption #> [1] "Note. This table presents Pearson correlation coefficients with pairwise deletion. N=4 missing Sex. N=404 missing Income. N=7 missing Height. N=179 missing iq. N=8 missing depressT. N=8 missing anxT. Group differences for continuous and categorical\n variables are indicated by t-statistic/ANOVA F\n and chi-squared, respectively. * p<.05, ** p<.01, *** p<.001" #>
correltable( data = psydat, vars = c("Age", "Height", "iq"), tri = "lower", html = TRUE )
#> <table class='gmisc_table' style='border-collapse: collapse; margin-top: 1em; margin-bottom: 1em;' > #> <thead> #> <tr><th style='border-bottom: 1px solid grey; border-top: 2px solid grey;'></th> #> <th style='font-weight: 900; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>Age</th> #> <th style='font-weight: 900; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>Height</th> #> <th style='font-weight: 900; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>iq</th> #> </tr> #> </thead> #> <tbody> #> <tr> #> <td style='text-align: left;'>Age</td> #> <td style='text-align: center;'></td> #> <td style='text-align: center;'></td> #> <td style='text-align: center;'></td> #> </tr> #> <tr> #> <td style='text-align: left;'>Height</td> #> <td style='text-align: center;'>.41***</td> #> <td style='text-align: center;'></td> #> <td style='text-align: center;'></td> #> </tr> #> <tr> #> <td style='border-bottom: 2px solid grey; text-align: left;'>iq</td> #> <td style='border-bottom: 2px solid grey; text-align: center;'>.09***</td> #> <td style='border-bottom: 2px solid grey; text-align: center;'>.04*</td> #> <td style='border-bottom: 2px solid grey; text-align: center;'></td> #> </tr> #> </tbody> #> <tr><td colspan='4' style='text-align: left;'> #> Note. This table presents Pearson correlation coefficients with pairwise deletion. N=7 missing Height. N=179 missing iq. * p<.05, ** p<.01, *** p<.001</td></tr> #> </table>
correltable( data = psydat, vars = c("Age", "Height", "iq"), tri = "lower", html = TRUE, strata = "Sex" )
#> <table class='gmisc_table' style='border-collapse: collapse; margin-top: 1em; margin-bottom: 1em;' > #> <thead> #> <tr><th style='border-bottom: 1px solid grey; border-top: 2px solid grey;'></th> #> <th style='font-weight: 900; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>Age</th> #> <th style='font-weight: 900; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>Height</th> #> <th style='font-weight: 900; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>iq</th> #> </tr> #> </thead> #> <tbody> #> <tr> #> <td style='text-align: left;'>Age</td> #> <td style='text-align: center;'></td> #> <td style='text-align: center;'>.43***</td> #> <td style='text-align: center;'>.07***</td> #> </tr> #> <tr> #> <td style='text-align: left;'>Height</td> #> <td style='text-align: center;'>.40***</td> #> <td style='text-align: center;'></td> #> <td style='text-align: center;'>.01</td> #> </tr> #> <tr> #> <td style='border-bottom: 2px solid grey; text-align: left;'>iq</td> #> <td style='border-bottom: 2px solid grey; text-align: center;'>.10***</td> #> <td style='border-bottom: 2px solid grey; text-align: center;'>.07***</td> #> <td style='border-bottom: 2px solid grey; text-align: center;'></td> #> </tr> #> </tbody> #> <tr><td colspan='4' style='text-align: left;'> #> Note. This table presents Pearson correlation coefficients with pairwise deletion. N=7 missing Height. N=179 missing iq. The data were split by Sex (upper triangle = F; lower triangle = M). * p<.05, ** p<.01, *** p<.001</td></tr> #> </table>
correltable( data = psydat, vars = c("Age", "Height", "iq"), var_names = c("Age (months)", "Height (inches)", "IQ"), tri = "upper", colnum = TRUE, html = TRUE )
#> <table class='gmisc_table' style='border-collapse: collapse; margin-top: 1em; margin-bottom: 1em;' > #> <thead> #> <tr><th style='border-bottom: 1px solid grey; border-top: 2px solid grey;'></th> #> <th style='font-weight: 900; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>1</th> #> <th style='font-weight: 900; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>2</th> #> <th style='font-weight: 900; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>3</th> #> </tr> #> </thead> #> <tbody> #> <tr> #> <td style='text-align: left;'>1. Age (months)</td> #> <td style='text-align: center;'></td> #> <td style='text-align: center;'>.41***</td> #> <td style='text-align: center;'>.09***</td> #> </tr> #> <tr> #> <td style='text-align: left;'>2. Height (inches)</td> #> <td style='text-align: center;'></td> #> <td style='text-align: center;'></td> #> <td style='text-align: center;'>.04*</td> #> </tr> #> <tr> #> <td style='border-bottom: 2px solid grey; text-align: left;'>3. IQ</td> #> <td style='border-bottom: 2px solid grey; text-align: center;'></td> #> <td style='border-bottom: 2px solid grey; text-align: center;'></td> #> <td style='border-bottom: 2px solid grey; text-align: center;'></td> #> </tr> #> </tbody> #> <tr><td colspan='4' style='text-align: left;'> #> Note. This table presents Pearson correlation coefficients with pairwise deletion. N=7 missing Height (inches). N=179 missing IQ. * p<.05, ** p<.01, *** p<.001</td></tr> #> </table>
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 )
#> <table class='gmisc_table' style='border-collapse: collapse; margin-top: 1em; margin-bottom: 1em;' > #> <thead> #> <tr><th style='border-bottom: 1px solid grey; border-top: 2px solid grey;'></th> #> <th style='font-weight: 900; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>Depression T</th> #> <th style='font-weight: 900; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>Anxiety T</th> #> </tr> #> </thead> #> <tbody> #> <tr> #> <td style='text-align: left;'>Age (months)</td> #> <td style='text-align: center;'>.02</td> #> <td style='text-align: center;'>-.01</td> #> </tr> #> <tr> #> <td style='text-align: left;'>Height (inches)</td> #> <td style='text-align: center;'>-.01</td> #> <td style='text-align: center;'>-.01</td> #> </tr> #> <tr> #> <td style='border-bottom: 2px solid grey; text-align: left;'>IQ</td> #> <td style='border-bottom: 2px solid grey; text-align: center;'>-.08***</td> #> <td style='border-bottom: 2px solid grey; text-align: center;'>-.06***</td> #> </tr> #> </tbody> #> <tr><td colspan='3' style='text-align: left;'> #> Note. This table presents Pearson correlation coefficients with pairwise deletion. N=7 missing Height (inches). N=179 missing IQ. N=8 missing Depression T. N=8 missing Anxiety T. * p<.05, ** p<.01, *** p<.001</td></tr> #> </table>