DT2Mat_freqs: Convert a data table of allele frequencies into a matrix (or...

View source: R/DT2Mat_freqs.R

DT2Mat_freqsR Documentation

Convert a data table of allele frequencies into a matrix (or vice versa)

Description

Takes a data table of allele frequencies in long format and converts it into a matrix in wide format (loci in columns and populations in rows). The reverse can also be done. See also DT2Mat_genos for converting matrix of genotypes.

Usage

DT2Mat_freqs(
  dat,
  popCol = "POP",
  locusCol = "LOCUS",
  freqCol = "FREQ",
  flip = FALSE
)

Arguments

dat

Data table or Matrix: The object to transform. If this is a long data table of allele frequencies, then three columns are required:

  1. The population ID (see param popCol).

  2. The locus ID (see param locusCol).

  3. The Ref allele frequency (see param freqCol).

The population pool column serves as the pivot point to convert the long data table into a wide matrix. If converting from a frequency matrix to a data table, see argument flip.

popCol

Character: The column name with the population information. Default is 'POP'.

locusCol

Character: The column name with the locus information. Default is 'LOCUS'.

freqCol

Character: The column name with the Ref allele frequency. Default is 'FREQ'.

flip

Logical: Instead of converting a (long) data table to a (wide) matrix, should a (wide) matrix be converted into a (long) data table? Default = FALSE. If TRUE, then param dat must be a matrix, with loci names as column headers, population IDs in the row names, and frequencies in the cells. When TRUE, params popCol, locusCol, and freqCol become void.

Value

When flip=FALSE, converts a data table into a frequency matrix. When flip=TRUE, converts a matrix into a data table with three columns: (1) $POP, the population ID (as for pool-seq data); (2) $LOCUS, the locus ID; and (3) FREQ, the Ref allele frequency.

Examples

data(data_PoolFreqs)

# Convert a long data table to a wide matrix
freqMat <- DT2Mat_freqs(
   dat=data_PoolFreqs,
   popCol='POOL',
   locusCol='LOCUS',
   freqCol='FREQ',
   flip=FALSE)

freqMat

# Convert a wide matrix back to a data table
freqDT <- DT2Mat_freqs(
   freqMat,
   popCol='POOL',
   locusCol='LOCUS',
   freqCol='FREQ',
   flip=TRUE)

freqDT


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