Skip to content

Instantly share code, notes, and snippets.

@andreagrioni
Created December 1, 2021 08:42
Show Gist options
  • Save andreagrioni/44021adb0a8d4e6319fa64de3b40e00a to your computer and use it in GitHub Desktop.
Save andreagrioni/44021adb0a8d4e6319fa64de3b40e00a to your computer and use it in GitHub Desktop.
# Create PCA object for PCAtools package
# select columns of interest
# and pivot wider
measuraments_annotated %>%
dplyr::select(
dplyr::all_of(c("SeqId", "q_norm", "SampleId"))) %>%
tidyr::pivot_wider(names_from=SeqId, values_from=q_norm) %>%
dplyr::distinct() -> metrics_df
# retrieve measurement and transpose to matrix
metrics_df %>%
dplyr::select(-SampleId) %>%
base::as.matrix() %>%
base::t() -> matrix
# assign colnames as sampleids
colnames(matrix) <- metrics_df$SampleId
# create meta data
measuraments_annotated %>%
dplyr::select(
dplyr::all_of(
c(
"SampleId", "SUBJID", "AGE", "SEX", "PlateId",
"timepoint", "ARMCD", "arm.visit", "rash" ))) %>%
dplyr::distinct() %>%
base::as.data.frame() -> meta_df
# assign rownames as sample ids
rownames(meta_df) <- meta_df$SampleId
# create pca object
PCAtools::pca(mat = matrix, metadata=meta_df) -> p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment