This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
# read selected columns: specify columns through 'col_types' | |
readr::read_csv("iris.csv", col_types = cols_only( Species = col_factor(c("setosa", "versicolor", "virginica")))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sparse Matrices in R: https://slowkow.com/notes/sparse-matrix/ | |
library(igraph) | |
library(Matrix) | |
library(tidyverse) | |
#------------------------------- | |
# Adjacency matrix using Matrix | |
#------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-------------------------------- | |
# file operations with patterns | |
#-------------------------------- | |
# list/delete files output/{chr}.out such that {chr} = 1-10 (other files with {chr} = 11-22 are untouched) | |
ls output/{1..10}.* | |
#-------------------- | |
# misc. caveats |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ------------------ | |
# from bgen to bed | |
# ------------------ | |
plink2 --bgen myfile.bgen \ | |
# filter by chr or by snps names | |
--chr 1 --extract {params.snps} \ | |
--make-bed --out myoutput | |
# 1. plink will create temporary/intermediate files such as *-temporary.{pgen,psam,pvar} | |
# 2. sample ids in .fam will be `ID1_ID1` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# print NAs as blank: https://stackoverflow.com/a/44348232 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sorting keys before look up speeds up queries. | |
# data.table outperforms over base R and tidyverse | |
# | |
# Copied from: https://appsilon.com/fast-data-lookups-in-r-dplyr-vs-data-table/ | |
# | |
# Benchmarks: | |
#Unit: microseconds | |
# expr min lq mean median uq | |
# look_up_base() 50762.103 53147.945 105500.0550 60221.954 62802.136 | |
# look_up_dplyr() 47527.238 49218.827 99142.9265 53923.058 58269.192 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(magrittr) | |
data(mtcars) | |
lm(mpg ~ am, mtcars) %>% summary %$% coefficients | |
# Estimate Std. Error t value Pr(>|t|) | |
#(Intercept) 17.147368 1.124603 15.247492 1.133983e-15 | |
#am 7.244939 1.764422 4.106127 2.850207e-04 | |
z <- 4.106127 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
drop <- function(X) | |
{ | |
qr <- qr(X) | |
r <- qr$rank # matrix rank | |
cols_keep <- qr$pivot[seq(r)] # first r indices in `qr$pivot` | |
#cols_drop <- qr$pivot[-seq(r)] # last (ncol(X) - r) indices in `qr$pivot` | |
X[, cols_keep, drop = FALSE] # use drop = FALSE in single-column case | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# References | |
# - http://www.sthda.com/english/articles/31-principal-component-methods-in-r-practical-guide/118-principal-component-analysis-in-r-prcomp-vs-princomp/ | |
## inc | |
library(tidyverse) | |
library(pls) | |
library(ggplot2) | |
library(cowplot) | |
theme_set(theme_cowplot()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## examples to read tables by data.table::fread | |
# read selected columns | |
fread(my_file, select = c("ID", "FID")) | |
# read selected columns & specify colunm classes | |
fread(my_file, select = c("ID", "FID"), colClasses = c(IID = "character")) |
NewerOlder