Skip to content

Instantly share code, notes, and snippets.

View xiaodaigh's full-sized avatar

evalparse xiaodaigh

View GitHub Profile
@xiaodaigh
xiaodaigh / benchmark_2048.jl
Created October 15, 2018 04:06
2048 Simulation challenge
using StatsBase
const DIRS = [:left, :up, :right, :down]
function init_game()
grid = zeros(Int8,4,4)
grid[rand(1:4),rand(1:4)] = rand2_1()
grid[rand(1:4),rand(1:4)] = rand2_1()
grid
end
@xiaodaigh
xiaodaigh / 1_forwardflag.r
Last active December 20, 2017 21:02
Fast implementation of binary (true/false) forward looking flag
forwardflag <- function(bools, ...) {
if(typeof(bools) != "boolean") {
warning("input variable not of boolean type, the only other accepted type is 0 & 1")
}
forwardflag_(bools, ...)
}
forwardflag_ <- function(bools, period = 12) {
stopifnot(period > 0)
@xiaodaigh
xiaodaigh / ctree_kmeans_iris_model_assessment
Created October 15, 2017 23:51
ctree vs kmeans on the iris dataset
# data prep ---------------------------------------------------------------
library(data.table)
data(iris)
iris_copy <- copy(iris)
setDT(iris_copy)
iris_copy_ctree <- copy(iris_copy)
# ctree model -------------------------------------------------------------
`infix_fn` <- function(left, right) {
#...some code
}
@xiaodaigh
xiaodaigh / feeatherc.r
Created August 28, 2017 22:47
Some R to read .feather data chunkwise
# featherc
library(data.table)
library(feather)
library(future)
library(dplyr)
plan(multiprocess)
options(future.globals.maxSize = Inf)
split_feather <- function(feather_file, by = NULL, parts = parallel::detectCores()) {
system.time(inputdata <- feather::read_feather(feather_file))
@xiaodaigh
xiaodaigh / how_to_use_split_into_columns.sas
Last active August 25, 2017 05:22
A SAS macro to split a dataset (.sas7bdat) into datasets where each resultant dataset contains exactly one column of the original data
data a;
do i = 1 to 10000;
b = i;
output;
end;
run;
%include "path/to/split_into_columns.sas";
libname outlib "path/to/output/dataset/";
@xiaodaigh
xiaodaigh / README.md
Last active February 1, 2017 02:15
== actionButton styling == The Twitter Bootstrap allows for many class styles for buttons. http://getbootstrap.com/2.3.2/base-css.html#buttons Added support for these styles to the actionButton.
@xiaodaigh
xiaodaigh / server.r
Last active February 2, 2016 18:07
R Shiny: An textInput that only gets invalidated upon losing focus or when enter is pressed shiny::runGist("7150112")
library(shiny)
shinyServer(function(input, output, session) {
# Partial example
output$meh <- renderPrint({
print("Press enter or focusout to update --- ")
print(input$myTextInput )
@xiaodaigh
xiaodaigh / intro.r
Created January 22, 2014 12:26
Intro to R
# reading and writing files
# press ctrl+enter to send
iris
fix(iris)
write.csv(iris,"c:/temp/iris.csv")
iris2 = read.csv("c:/temp/iris.csv")
nrow(iris2)
# get help
data <- read.csv("c:/bb2.csv")
cor.data <- cor(data[sapply(data,typeof)=="double"],method="spearman")
dist <- function(y,x) {
if(is.null(dim(x))) {
sqrt((x[1] - y[1])^2 + (x[2] - y[2])^2)
} else {
sqrt((x[,1] - y[1])^2 + (x[,2] - y[2])^2)
}