Skip to content

Instantly share code, notes, and snippets.

View klmr's full-sized avatar
📦
Making your R code easier to reuse

Konrad Rudolph klmr

📦
Making your R code easier to reuse
View GitHub Profile
@klmr
klmr / relatedness.r
Created November 12, 2019 18:07
Average number of shared alleles between two siblings
n_genes = 20000
n = 100
relatedness = replicate(n, {
child1 = sample(c('mother', 'father'), n_genes, replace = TRUE)
child2 = sample(c('mother', 'father'), n_genes, replace = TRUE)
mean(child1 == child2)
})
mean(relatedness)
@klmr
klmr / freq_table.cpp
Created October 23, 2019 12:38
Frequency table and mode calculation
#include <algorithm>
#include <initializer_list>
#include <iostream>
#include <iterator>
#include <string_view>
#include <type_traits>
#include <unordered_map>
namespace stats {
@klmr
klmr / errors.r
Last active August 30, 2019 02:00
Simple error handling with subclasses in R. Code is a direct port of Python code from https://codereview.stackexchange.com/q/225419/308
os_error = function (message, call = NULL) {
class = c('os_error', 'error', 'condition')
structure(list(message = message, call = call), class = class)
}
value_error = function (message, call = NULL) {
class = c('value_error', 'error', 'condition')
structure(list(message = message, call = call), class = class)
}
@klmr
klmr / fib.py
Last active September 14, 2022 08:39
Efficient, tail recursive fibonacci implementation for R and Python (but since they doesn’t do TCO it’s still using O(n) space)
def fib(n: int) -> int:
def f(n, a, b):
if n == 0: return a
if n == 1: return b
return f(n - 1, b, a + b)
return f(n, 0, 1)
@klmr
klmr / Calculator.cs
Created March 6, 2019 14:49
Workaround for missing numeric type constraints in C# — https://stackoverflow.com/q/32664/1968
using System;
using System.Collections.Generic;
namespace OperatorTest {
public interface ICalculator { }
public interface ICalculator<T> : ICalculator {
T Add(T a, T b);
T Divide(T a, T b);
T Multiply(T a, T b);
@klmr
klmr / inspector-stylesheet.css
Created February 27, 2018 16:07
Open the inspector and add the following as a new CSS rule (once you’ve added any rule, you can subsequently edit the inspector stylesheet found under “Sources” on Chrome)
.blob-code span:before {
background: #fdfd9c;
color: #a2a2a2;
content: attr(class);
font-family: sans-serif;
font-size: 0.7em;
padding: 0 0.2em;
vertical-align: middle;
}
@klmr
klmr / look-ma-no-downcasts.cs
Last active December 7, 2020 16:10
Using generic interfaces in class hierarchies without downcasts (discussed on https://softwareengineering.stackexchange.com/a/366666/2366)
using System;
using Color = System.Drawing.Color;
namespace CsTest {
// Generic interfaces. // (1)
interface IEquatable<in T> {
bool Equals(T other);
}
interface ICloneable<out T> {
  • Invalid signature void main() (… I mean, seriously?!)
  • Missing namespace qualifiers throughout
  • Missing include for accumulate
  • Missing ) in accumulate call
  • Invalid arguments to accumulate call:
    • init missing
    • op has wrong function type (expected (T, T) -> T; got (T) -> void)
    • — in fact, it seems they actually meant std::for_each.
  • … but using std::accumulate correctly would have been way easier, without a gratuitous lambda.
@klmr
klmr / summary-tibble.r
Last active February 12, 2018 12:14
Summarise a column in a (grouped) tibble
summary.tbl_df = function (object, ...) {
dots = rlang::quos(...)
stopifnot(length(dots) == 1)
name = rlang::quo_name(dots[[1]])
do(object, as_data_frame(as.list(summary(.[[name]]))))
}
@klmr
klmr / tmup
Last active February 6, 2018 13:26 — forked from simonjbeaumont/tmup
Update bash to latest tmux environment on reattaching.
#!/bin/bash
tmup() {
local IFS=$'\n'
for line in $(tmux showenv -t $(tmux display -p '#S')); do
case $line in
-*) unset ${line:1} ;;
*) export $line ;;
esac
done