pairwiseMat2DT: Convert a pairwise matrix into a pairwise data table

View source: R/pairwiseMat2DT.R

pairwiseMat2DTR Documentation

Convert a pairwise matrix into a pairwise data table

Description

This function takes pairwise matrix (all possible combinations in rows and columns) and converts it into a long-format data table. This function can also be flipped, i.e., going from a pairwise data table to a pairwise matrix.

Usage

pairwiseMat2DT(dat, flip = FALSE, X1, X2, Y, diagAdd = TRUE, diagVal = 0)

Arguments

dat

Matrix or Data table: The default parameterisation (flip==FALSE) is expecting a pairwise matrix. This matrix needs to be symmetrical, with a values on the diagonal representing comparisons within a subject, and values on the off-diagonal containing the measures between subject pairs. There should be row and column names. If instead you want to go in the flipped parameterisation (flip==TRUE), then a data table is expected, where pair combinations are represented into two columns and the measured variable is in a third column. In this second parameterisation, the data table does not need to have within-subject comparisons (diagonals; you can add these in).

flip

Logical: Should the function be flipped? Default is FALSE, a pairwise matrix should be converted into a pairwise data table. If set to TRUE, then a pairwise data table is converted into a matrix.

X1

Character: The column name for the first member of the pair. If flip==FALSE, then X1 will take the row names from the matrix and put them into the first column of the output data table. If flip==TRUE, then X1 will be take the column in the data table and put it as row names in the output matrix.

X2

Character: The column name for the first member of the pair. If flip==FALSE, then X2 will take the column names from the matrix and put them into the second column of the output data table. If flip==TRUE, then X2 will be take the column in the data table and put it as column names in the output matrix.

Y

Character: The column name or the measured variable between pairs. If flip==FALSE, then Y will take the values in the off-diagonal in the matrix and put them into the third column of the output data table. If flip==TRUE, then Y will be take the column in the data table and put it as values in the off-diagonal in the output matrix.

diagAdd

Logical: Should the diagonal be added? Default is TRUE. Only takes effect if flip==TRUE.

diagVal

Numeric: The value to add along the diagonal when diagAdd==TRUE. Default is 0.

Examples

library(genomalicious)
data(data_Genos)

# Calculate pairwise FST
pairFst <- fstat_calc(data_Genos, type='genos', fstat='FST', global=FALSE, pairwise=TRUE)

# Convert into a pairwise matrix
pairMat <- pairwiseMat2DT(
   dat=pairFst$genome, flip=TRUE, X1='POP1', X2='POP2', Y='FST',
   diagAdd=TRUE, diagVal=0
)
pairMat

# Convert the pairwise matrix into a data table
pairDT <- pairwiseMat2DT(
   dat=pairMat, flip=FALSE, X1='POP1', X2='POP2', Y='FST'
)
pairDT


j-a-thia/genomalicious documentation built on Oct. 19, 2024, 7:51 p.m.