Skip to content

Instantly share code, notes, and snippets.

## Empty list -- just use the empty environment for this.
nil <- function() {
emptyenv()
}
## Test if a list is the empty list:
is_empty <- function(lis) {
identical(lis, nil())
}
@yamaguchiyuto
yamaguchiyuto / basic_plot.py
Last active March 7, 2022 15:51
Plot degree distribution (Freq, CDF, CCDF) from edgelist data
import sys
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
def plot(data,filename,degreetype):
""" Plot Distribution """
plt.plot(range(len(data)),data,'bo')
plt.yscale('log')
plt.xscale('log')
@tleonardi
tleonardi / tanimoto.R
Created February 23, 2015 11:52
Tanimoto distance in R
tanimoto <- function(x, similarity=F) {
res<-sapply(x, function(x1){
sapply(x, function(x2) {i=length(which(x1 & x2)) / length(which(x1 | x2)); ifelse(is.na(i), 0, i)})
})
if(similarity==T) return(res)
else return(1-res)
}
x <- data.frame(Samp1=c(0,0,0,1,1,1,0,0,1), Samp2=c(1,1,1,1,1,1,0,0,1), Samp3=c(1,0,0,1,0,0,0,0,0), Samp4=c(1,1,1,1,1,1,1,1,1))
tanimoto(x)
@dcomtois
dcomtois / r_c.txt
Last active January 10, 2024 19:08
Linking R internal & primitive C functions with appropriate source files. --- printname = R function name ; c.entry = C function ; sourcefile = relevant C source file; line_no: line featuring fn definition (not all checked)
printname c.entry file line_no
- do_arith R-4.0.3\src\main\arithmetic.c 411
! do_logic R-4.0.3\src\main\logic.c 40
!= do_relop R-4.0.3\src\main\relop.c 51
$ do_subset3 R-4.0.3\src\main\subset.c 1206
$<- do_subassign3 R-4.0.3\src\main\subassign.c 2081
%% do_arith R-4.0.3\src\main\arithmetic.c 411
%*% do_matprod R-4.0.3\src\main\array.c 1228
%/% do_arith R-4.0.3\src\main\arithmetic.c 411
& do_logic R-4.0.3\src\main\logic.c 40
@statguy
statguy / gist:3184ea6ff4ebb927c747
Last active February 16, 2018 16:50
INLA spatial parameters test
library(INLA)
m = 50
points = matrix(runif(m*2), m, 2)
mesh = inla.mesh.2d(loc=points,cutoff=0.05,offset=c(0.1, 0.4),max.edge=c(0.05, 0.5) )
bnd = inla.nonconvex.hull(points, convex=0.12)
mesh = inla.mesh.2d(boundary=bnd,cutoff=0.05,offset = c(1, 0.5),max.edge=c(0.1, 0.5) )
A = inla.spde.make.A(mesh, loc=points)
sigma0 = 1 ## Field std.dev. for theta=0
size = min(c(diff(range(mesh$loc[,1])), diff(range(mesh$loc[,2]))))
range0 = size/5 ## A fifth of the approximate domain width.
abstract Person
type Adult <: Person
age::Int
height::Float64
position::Int
end
type Child <: Person
age::Int
@jeroen
jeroen / fortran.R
Created September 16, 2015 10:11
Find CRAN packages with FORTRAN code
# Find all CRAN package with FORTRAN code
packages <- c()
base_url <- 'https://api.github.com/search/repositories?q=user:cran%20language:fortran'
for(page in 1:100){
url <- paste0(base_url, "&page=", page)
cat("Reading", url, "\n")
repos <- jsonlite::fromJSON(url)
if(!length(repos$items))
break
packages <- c(packages, repos$items$name)
@kyrcha
kyrcha / sigmoid.R
Last active November 26, 2019 08:13
Fitting a sigmoind curve in R
# function needed for visualization purposes
sigmoid = function(params, x) {
params[1] / (1 + exp(-params[2] * (x - params[3])))
}
x = 1:53
y = c(0,0,0,0,0,0,0,0,0,0,0,0,0,0.1,0.18,0.18,0.18,0.33,0.33,0.33,0.33,0.41,
0.41,0.41,0.41,0.41,0.41,0.5,0.5,0.5,0.5,0.68,0.58,0.58,0.68,0.83,0.83,0.83,
0.74,0.74,0.74,0.83,0.83,0.9,0.9,0.9,1,1,1,1,1,1,1)
@mick001
mick001 / copulas_example.R
Last active July 23, 2024 04:09
Modelling dependence with copulas. Full article at: http://datascienceplus.com/modelling-dependence-with-copulas/
#Load library mass and set seed
library(MASS)
set.seed(100)
# We are going to use 3 random variables
m <- 3
# Number of samples to be drawn
n <- 2000
@thomasp85
thomasp85 / animate.R
Created February 3, 2016 19:43
Animating graph over time
library(ggraph)
library(gganimate)
library(igraph)
# Data from http://konect.uni-koblenz.de/networks/sociopatterns-infectious
infect <- read.table('out.sociopatterns-infectious', skip = 2, sep = ' ', stringsAsFactors = FALSE)
infect$V3 <- NULL
names(infect) <- c('from', 'to', 'time')
infect$timebins <- as.numeric(cut(infect$time, breaks = 100))
# We want that nice fading effect so we need to add extra data for the trailing