cor_reshape: Reshape Correlation Data

cor_gatherR Documentation

Reshape Correlation Data

Description

Reshape correlation analysis results. Key functions:

  • cor_gather(): takes a correlation matrix and collapses (i.e. melt) it into a paired list (long format).

  • cor_spread(): spread a long correlation data format across multiple columns. Particularly, it takes the results of cor_test and transforms it into a correlation matrix.

Usage

cor_gather(data, drop.na = TRUE)

cor_spread(data, value = "cor")

Arguments

data

a data frame or matrix.

drop.na

logical. If TRUE, drop rows containing missing values after gathering the data.

value

column name containing the value to spread.

Functions

  • cor_gather(): takes a correlation matrix and collapses (or melt) it into long format data frame (paired list)

  • cor_spread(): spread a long correlation data frame into wide format. Expects the columns "var1", "var2" and "cor" in the data. (correlation matrix).

See Also

cor_mat(), cor_reorder()

Examples

# Data preparation
#::::::::::::::::::::::::::::::::::::::::::
mydata <- mtcars %>%
  select(mpg, disp, hp, drat, wt, qsec)
head(mydata, 3)

# Reshape a correlation matrix
#::::::::::::::::::::::::::::::::::::::::::
# Compute a correlation matrix
cor.mat <- mydata %>% cor_mat()
cor.mat

# Collapse the correlation matrix into long format
# paired list data frame
long.format <- cor.mat %>% cor_gather()
long.format

# Spread a correlation data format
#::::::::::::::::::::::::::::::::::::::::::
# Spread the correlation coefficient value
long.format %>% cor_spread(value = "cor")
# Spread the p-value
long.format %>% cor_spread(value = "p")

rstatix documentation built on Feb. 16, 2023, 6:10 p.m.