Skip to content

Instantly share code, notes, and snippets.

View lurodrigo's full-sized avatar

Luiz Rodrigo de Souza lurodrigo

View GitHub Profile
@lurodrigo
lurodrigo / matrices.ex
Created November 6, 2021 02:19
matrices
defmodule Matrices do
@type permutation_map :: %{integer => integer}
@spec build_permutation(Enumerable.t()) :: permutation_map
def build_permutation(l) do
n = Enum.count(l)
Enum.zip(1..n, l)
|> Map.new()
end
@lurodrigo
lurodrigo / ms.R
Created March 5, 2018 20:36
Multiple summarise() statements
ms = function(.data, ...) {
dots = quos(...)
map(dots, ~ call_modify(., .data = .data) %>% eval_tidy()) %>%
reduce(function(tb1, tb2) {
for (newcol in setdiff(names(tb2), names(tb1))) {
tb1[[newcol]] = tb2[[newcol]]
}
tb1
})
@lurodrigo
lurodrigo / dummify.R
Created March 5, 2018 18:30
Provides a convenient way to create dummy variables
# to-do: properly document it
library(tidyverse)
library(rlang)
dummify = function(.data, ..., .pattern = "{column}_{value}") {
cols = quos(...)
new_cols = imap(cols, function(quoted_expr, column) {
if (is_integer(column) || column == "") {