Skip to content

Instantly share code, notes, and snippets.

@arraytools
arraytools / embed.c
Created Nov 24, 2020
An example from Bioconductor workshop
View embed.c
#include <Rembedded.h>
#include <Rdefines.h>
static void doSplinesExample();
int
main(int argc, char *argv[])
{
Rf_initEmbeddedR(argc, argv);
doSplinesExample();
Rf_endEmbeddedR(0);
@arraytools
arraytools / MASS_vs_mvtnorm.R
Created Nov 24, 2020
R generate multivariate normal
View MASS_vs_mvtnorm.R
set.seed(1234)
junk <- biospear::simdata(n=500, p=500, q.main = 10, q.inter = 10,
prob.tt = .5, m0=1, alpha.tt= -.5,
beta.main= -.5, beta.inter= -.5, b.corr = .7, b.corr.by=25,
wei.shape = 1, recr=3, fu=2, timefactor=1)
## Method 1: MASS::mvrnorm()
## This is simdata() has used. It gives different numbers on different OS.
##
library(MASS)
set.seed(1234)
@arraytools
arraytools / randomData.R
Created Aug 27, 2020
test glmnet with random data
View randomData.R
library(glmnet)
# Binary data
n = 1000
p = 100
nzc = trunc(p/10)
for(i in 1:100) {
cat(i, " ")
if (i %% 10 == 0) cat("\n")
x = matrix(rnorm(n * p), n, p)
@arraytools
arraytools / readsum.R
Last active Mar 29, 2020
Three program languages to compute the sum of a numerical matrix (with column header)
View readsum.R
x <- read.delim("GX_datab31.txt")
dim(x)
# [1] 1579 463
sum(x)
sum(x[1, ])
options(digits = 20)
sum(x)
# [1] 12589441.434223400429
sum(x[1,])
# [1] 4787.087760000000344
@arraytools
arraytools / TPM_rsem_tximport_DESeq2.R
Last active Dec 11, 2019
import TPM for gene level analysis in DESeq2
View TPM_rsem_tximport_DESeq2.R
# This is a note about import rsem-generated file for DESeq2 package
# As described by the tximport's vignette, the method below uses the gene-level estimated counts from the quantification tools, and additionally to use the transcript-level abundance estimates to calculate a gene-level offset that corrects for changes to the average transcript length across samples.
# This approach corrects for potential changes in gene length across samples (e.g. from differential isoform usage)
# References:
# 1. http://bioconductor.org/packages/release/bioc/vignettes/tximport/inst/doc/tximport.html#rsem
# 2. https://bioconductor.org/packages/release/bioc/vignettes/DESeq2/inst/doc/DESeq2.html#differential-expression-analysis
@arraytools
arraytools / Rconsole
Last active Oct 1, 2019
Rconsole for Windows OS with black background. The font size is increased to 14.
View Rconsole
# Optional parameters for the console and the pager
# The system-wide copy is in rwxxxx/etc.
# A user copy can be installed in `R_USER'.
## Style
# This can be `yes' (for MDI) or `no' (for SDI).
MDI = yes
# the next two are only relevant for MDI
toolbar = yes
statusbar = no
View silentnight.R
# https://aschinchon.wordpress.com/2014/03/13/the-lonely-acacia-is-rocked-by-the-wind-of-the-african-night/
depth <- 9
angle<-30 #Between branches division
L <- 0.90 #Decreasing rate of branches by depth
nstars <- 300 #Number of stars to draw
mstars <- matrix(runif(2*nstars), ncol=2)
branches <- rbind(c(1,0,0,abs(jitter(0)),1,jitter(5, amount = 5)), data.frame())
colnames(branches) <- c("depth", "x1", "y1", "x2", "y2", "inertia")
for(i in 1:depth)
{
@arraytools
arraytools / perspExample.R
Last active Mar 21, 2019
3 D example using persp()
View perspExample.R
### Random pattern
# Create matrix with random values with dimension of final grid
rand <- rnorm(441, mean=0.3, sd=0.1)
mat.rand <- matrix(rand, nrow=21)
# Create another matrix for the colors. Start by making all cells green
fill <- matrix("green3", nr = 21, nc = 21)
# Change colors in each cell based on corresponding mat.rand value
fcol <- fill
View christmastree.R
# http://blogs.sas.com/content/iml/2012/12/14/a-fractal-christmas-tree/
# Each row is a 2x2 linear transformation
# Christmas tree
L <- matrix(
c(0.03, 0, 0 , 0.1,
0.85, 0.00, 0.00, 0.85,
0.8, 0.00, 0.00, 0.8,
0.2, -0.08, 0.15, 0.22,
-0.2, 0.08, 0.15, 0.22,
0.25, -0.1, 0.12, 0.25,
@arraytools
arraytools / track_R_package_download.R
Last active Mar 4, 2019
Get top downloaded R packages from a certain time period
View track_R_package_download.R
## ======================================================================
## Step 1: Download all log files
## ======================================================================
# Here's an easy way to get all the URLs in R
start <- as.Date('2018-07-01')
today <- as.Date('2018-12-31')
all_days <- seq(start, today, by = 'day')
You can’t perform that action at this time.