Skip to content

Instantly share code, notes, and snippets.

@abikoushi
abikoushi / query_awk.R
Created December 9, 2024 09:52
Run AWK query from R (change specific rows)
query_awk <- function(vpar, pdir, filename, uid){
chl <- paste0("cat ", pdir, "/", filename)
for(i in 1:nrow(vpar)){
chr <- paste0("\"", paste(vpar[i,], collapse = ", "), "\"")
awk <- paste0(" | awk 'NR==", uid[i], "{sub('/.*/',", chr, ")}1'")
chl <- paste0(chl, awk)
}
chl <- paste0(chl, " > ", pdir, "/tmp.csv")
mvit <- paste0("mv ", pdir, "/tmp.csv " , pdir, "/", filename)
system(chl)
@abikoushi
abikoushi / binormal.r
Created December 6, 2024 07:13
base's `persp` and `contour` example
x = seq(-4,4,length.out=100)
y = seq(-4,4,length.out=100)
df <- expand.grid(x = x, y = y)
prob <- matrix(pnorm(df$x)*pnorm(df$y), 100, 100)
#png("persp.png")
persp(x = x, y = y, z = prob, theta = 20, phi=45)
#dev.off()
#png("contour.png")
@abikoushi
abikoushi / rotate_dino.R
Created December 3, 2024 23:06
rotation matrix with dino
library(datasauRus)
library(ggplot2)
library(dplyr)
library(gganimate)
rot <-function(th) {
matrix(c(cos(th),-sin(th),
sin(th),cos(th)), byrow = TRUE, nrow = 2)
}
@abikoushi
abikoushi / glm_pois.R
Created November 24, 2024 02:19
Comparison the two ratios
n <- 1000
rate <- 1
set.seed(1234)
tau_x <- rgamma(n,1,1)
X <- rpois(n,tau_x*rate)
tau_y <- rgamma(n,2,1)
Y <- rpois(n,tau_y*rate)
#png("ECDF_rate.png")
curve(ecdf(X/tau_x)(x), type = "s", xlim = c(0, 15), ylim=c(0,1), col="royalblue", ylab = "ECDF", xlab = "ratio")
curve(ecdf(Y/tau_y)(x), type = "s", add=TRUE, col="orangered", lty=2)
@abikoushi
abikoushi / Chung2017.R
Created November 21, 2024 22:12
Comparison plots for parallel coordinate vs. UMAP
library(Matrix)
library(readr)
library(tidyr)
library(dplyr)
library(ggplot2)
library(umap)
library(patchwork)
#https://www.weizmann.ac.il/sites/3CA/breast
cells <- read_csv("./data/Data_Chung2017_Breast/Cells.csv")
@abikoushi
abikoushi / pcp_jitter.R
Created November 17, 2024 05:29
Parallel coordinate plot with jitter
library(dplyr)
library(tidyr)
library(ggplot2)
df1 <- mutate(iris, id=1:n()) %>%
pivot_longer(1:4)
ggplot(df1,aes(x=name,y=value,group=id, colour = Species))+
geom_line(alpha=0.5)+
guides(colour=guide_legend(override.aes = list(alpha=1, linewidth=1)))+
@abikoushi
abikoushi / parcoord_dia.r
Created November 7, 2024 10:15
`parcoord` chart in R
library(dplyr)
library(MASS)
data("diamonds", package = "ggplot2")
diamonds2 <- dplyr::select(diamonds, carat, depth:price,x:z)
a01 <- rgb(0,0,0,0.1)
png("pairs.png")
plot(diamonds2, col=a01)
dev.off()
@abikoushi
abikoushi / stepline.R
Created November 3, 2024 01:08
parallel coordinate plot with step-line
library(dplyr)
library(tidyr)
library(ggplot2)
library(patchwork)
df1 <- mutate(iris,id=1:n()) %>%
pivot_longer(1:4)
df2 <-reframe(df1, x=c(as.integer(factor(name))-0.25,as.integer(factor(name))+0.25),
@abikoushi
abikoushi / dataloader_mtx.r
Created October 31, 2024 02:25
read specific rows on text file
scan1_mtx <- function(con, skip = 0){
base::scan(con, nmax = 1, quiet=TRUE,
what = list(i=integer(), j=integer(), v=numeric()),
skip = skip)
}
dataloader_mtx2 <- function(file_path, bag){
con <- file(file_path, open = "r") #Open for reading in text mode
#get matrix size
@abikoushi
abikoushi / sumouterprod2.cpp
Created October 11, 2024 03:57
Sum of the all of the outer product (for Rcpp)
#include "RcppArmadillo.h"
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
using namespace arma;
// [[Rcpp::export]]
double sumouterprod(const arma::field<arma::vec> & V){
int K = V.n_rows;
arma::vec tout = V(0);
for(int j=1; j<K; j++){