R/getPC1andPC2.R

Defines functions getPC1andPC2

Documented in getPC1andPC2

#' Get PC1 and PC2 of input data frame.
#' 
#' Calculate first two PCs and return a data frame
#'
#' @param df An input data frame
#' @return A data frame where first column is a vector representing PC1, while second column is a vector representing PC2
#' @author Alexander Kheirallah ([email protected])
#' @export
#'


getPC1andPC2 <- function(df){
  o <- as.data.frame(matrix(ncol = 2, nrow = ncol(df)))
  SVD <- svd(df - rowMeans(df))
  o[,1] <- SVD$v[,1]
  o[,2] <- SVD$v[,2]
  colnames(o) <- c("PC1", "PC2")
  row.names(o) <- colnames(df)
  return(o)
} 
msxakk89/dat documentation built on April 3, 2020, 5:39 p.m.