Skip to content

Instantly share code, notes, and snippets.

View alekrutkowski's full-sized avatar

alek alekrutkowski

View GitHub Profile
@alekrutkowski
alekrutkowski / factorsToDummies.R
Last active February 16, 2016 13:13
Convert an R data.frame with factors into a data.frame with dummies (non-factor columns unchanged)
library(magrittr)
factorsToDummies <- function(df)
# df: a data.frame.
# returns: a data.frame with non-factor columns unchanged
# and factor columns replaced by a series of dummies
# for each factor.
lapply(df,
function(x)
if (x %>% is.factor %>% not) x else
@alekrutkowski
alekrutkowski / mlapply.R
Last active December 30, 2020 11:42
mapply for all combinations of arguments or lapply for multiple vectors/lists of arguments
library(magrittr)
# A more efficient implementation avoiding
# expand.grid which copies the parameters/arguments multiple times,
# which is inefficient for large parameters (e.g. data.frames).
mlapply <- function(.Fun, ..., .Cluster=NULL, .parFun=parallel::parLapply) {
`--List--` <-
list(...)
names(`--List--`) <-
names(`--List--`) %>%
@alekrutkowski
alekrutkowski / cond.R
Last active June 10, 2016 09:31
Clojure-style cond macro in R
library(magrittr)
cond <- function(...) {
# Clojure-style cond macro in R -- creates nested if-else calls
# arguments: pairs -- condition1, what-if-true1,
# condition2, what-if-true2,
# etc...
# what-if-all-contitions-false
e <- parent.frame()
substitute(list(...)) %>%
@alekrutkowski
alekrutkowski / cond_case.R
Created June 13, 2016 11:33
Clojure-style cond and case macros in R
library(magrittr)
# Clojure-style cond and case macros in R
# Inspired by
# https://clojuredocs.org/clojure.core/cond and
# https://clojuredocs.org/clojure.core/case.
# See the examples in the bottom.
cond_case_Factory <- function(ENV, obj, message_infix, vectorised, comparFun, ...) {
IF <- if (vectorised)
@alekrutkowski
alekrutkowski / cachedCall.R
Last active June 15, 2016 12:45
A simple caching system for R (persistent, disk-based)
library(magrittr)
stopifnot('digest' %in% installed.packages()[,"Package"])
if (!dir.exists('.cache.db')) dir.create('.cache.db') # in the current working directory
# Public API --------------------------------------------------------------
cachedCall <-
function(`fun*`,
..., # file paths/names in args in ... need to be wrapped in File()
@alekrutkowski
alekrutkowski / split_into.R
Last active August 9, 2016 14:47
A wrapper for base::split taking as an argument the expected number of sub-elements
library(magrittr)
split_into <- function(x, n, sorted=TRUE)
## x -- a vector (atomic or list)
## n -- the number of elements (groups)
## returns a list with n elements
## each containing some of the elements of x
n %>%
seq_len %>%
rep.int(x %>%
length %>%
@alekrutkowski
alekrutkowski / CCA.R
Created August 5, 2016 14:37
cachedCall&Assign for
library(cacheflow) # https://github.com/alekrutkowski/cacheflow
CCA <- function(FUN,...)
## cachedCall&Assign
## A macro to avoid repetitive typing like
## myX_result <- cachedCall(myX_function, ...)
## creates and object `.myX` to avoid overwriting function `myX`
## that still may be used in the subsequent cachedCall/CCA calls
## i.e.
## CCA(myX, ...)
@alekrutkowski
alekrutkowski / default.md
Last active August 24, 2016 09:04
R function for getting the default value of a function argument
default <- function(sym) {
    Sym <- substitute(sym)
    if (!(is.symbol(Sym)))
        stop('\nExpected a symbol as an argument, got a ', class(sym))
    ArgList <- eval.parent(quote(formals()))
    ArgName <- as.character(Sym)
    if (!(ArgName %in% names(ArgList)))
        stop('\nThere is no argument named `', ArgName,'`')
 if (is.symbol(ArgList[[ArgName]]))
@alekrutkowski
alekrutkowski / gentest.md
Last active September 1, 2016 10:36
Boilerplate for generative property testing in R

Function for generative property testing in R

library(magrittr)
if (!('memoise' %in% installed.packages()[,"Package"]))
    stop('Package "memoise" is needed.')

gentest <- function(f, ...) {
@alekrutkowski
alekrutkowski / Data imports for TAF.R
Last active July 10, 2020 10:07
Data imports from Eurostat to ECFIN.B2's Tax Assessment Framework
library(magrittr)
# requires also `eurodata` package
# https://github.com/alekrutkowski/eurodata
# Helpers -----------------------------------------------------------------
msg <- function(obj, txt='\n') {
message(txt)
obj