pca2DT: Convert a 'prcomp' object to a long-format 'data.table'

View source: R/pca2DT.R

pca2DTR Documentation

Convert a prcomp object to a long-format data.table

Description

Convert a prcomp object to a long-format data.table

Usage

pca2DT(pcaObj, pops = NULL, subAxes = NULL)

Arguments

pcaObj

Prcomp: An object produced by prcomp. If produced from genomalicious::pca_genos, it may contain a $pops indexed item, which contains the population designations for each sample in pcaObj$x.

pops

Character: An optional vector of population IDs. Default is NULL. Be sure to check that they match samples in rows of pcaObj$x.

subAxes

Integer: An optional vector of values to subset the PC axes. Default is NULL.

Value

Returns a long-format data.table with columns $POP, the population IDs (if data available), $SAMPLE, the sample IDs, $PC[x], the individual PC axes, with [x] denoting the axis number.

Examples

library(genomalicious)
data(data_Genos)

# With populations
PCA.pops <- pca_genos(data_Genos, popCol='POP')

pca2DT(PCA.pops) %>% print

# Without populations and all axes (default)
PCA.nopops <- pca_genos(data_Genos)

pca2DT(PCA.nopops) %>% print

# Manual populations, with just the first three axes
pops <- rownames(PCA.nopops$x) %>%
   sub('Ind', '', .) %>%
   sub('_.*', '', .)

pops

pca2DT(PCA.nopops, pops=pops, subAxes=1:3) %>% print


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