Skip to content

Instantly share code, notes, and snippets.

@abikoushi
abikoushi / radar_iris.r
Created January 15, 2025 04:54
radar chart using package "see"
require("tidyr")
require("dplyr")
require("ggplot2")
require("see")
dfiris <- mutate(iris, id=row_number()) %>%
pivot_longer(Sepal.Length:Petal.Width)
#head(dfiris)
@abikoushi
abikoushi / statfuns.R
Created December 30, 2024 13:34
An example of line chart with markers
library(ggplot2)
library(dplyr)
lr_default <- function(t, delay, forgetting){
(t+delay)^(-forgetting)
}
linedf <- expand.grid(delay=c(1,2), forgetting=c(0.6,0.9), t=seq(0,20, length.out=50)) %>%
mutate(lr=lr_default(t, delay, forgetting)) %>%
mutate(param = paste0("delay=",delay, ", ", "forgetting=", forgetting)) %>%
@abikoushi
abikoushi / bump.R
Created December 25, 2024 14:52
bump plot with jitter
library(ggplot2)
library(magrittr)
library(tidyr)
library(dplyr)
sigmoid2 <- function(x_from, x_to, y_from, y_to, smooth = 5, n = 100, direction = "x", location = 0, scale = 1) {
if(!direction %in% c("x", "y")) {stop("Only the directions x or y is allowed.")}
if(direction == "x") {
x <- seq(-smooth, smooth, length = n)
@abikoushi
abikoushi / datasauRus_svd.R
Created December 14, 2024 04:04
Singular value decomposition of datasauRus
library(datasauRus)
library(ggplot2)
library(dplyr)
X = dplyr::filter(datasaurus_dozen,dataset=="dino") %>%
dplyr::select(-dataset) %>%
as.matrix()
res_svd <- svd(as.matrix(X))
df_svd <- bind_rows(data.frame(res_svd$u, matrix="U"),
@abikoushi
abikoushi / datasauRus.R
Created December 13, 2024 13:15
rotate DataSaurus
library(datasauRus)
library(ggplot2)
library(dplyr)
library(gganimate)
rot <-function(ti, period=16) {
coeff <- 2*pi/period
matrix(c(cos(coeff*ti),-sin(coeff*ti),
sin(coeff*ti), cos(coeff*ti)), byrow = TRUE, nrow = 2)
}
@abikoushi
abikoushi / readmtx.cpp
Last active December 11, 2024 04:29
read multiple lines from mtx file (Rcpp)
#include "RcppArmadillo.h"
// [[Rcpp::depends(RcppArmadillo)]]
#include <iostream>
#include <string>
#include <vector>
using namespace Rcpp;
void readmtx(arma::uvec & row_i,
arma::uvec & col_i,
@abikoushi
abikoushi / readaline.cpp
Created December 10, 2024 03:34
get a line from csv (Rcpp)
#include "RcppArmadillo.h"
// [[Rcpp::depends(RcppArmadillo)]]
#include <iostream>
#include <string>
#include <vector>
using namespace Rcpp;
// [[Rcpp::export]]
std::vector<double> readaline(std::string readtxt, int ind_n) {
std::ifstream file(readtxt);
@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)
}