Skip to content

Instantly share code, notes, and snippets.

View alekrutkowski's full-sized avatar

alek alekrutkowski

View GitHub Profile
@alekrutkowski
alekrutkowski / .Web scraping Polish Parliament (Sejm), R script
Last active July 10, 2020 13:06
Web scraping Polish Parliament (Sejm), 8th and 9th term, years 2018–2019 (R script)
# just a stub to change the title of the gist
@alekrutkowski
alekrutkowski / excelColsToNums.R
Last active November 19, 2020 15:23
Convert Excel column letter codes to numbers (inverse of `openxlsx::int2col`)
# Convert Excel column letter codes to numbers
# Very similar to openxlsx::convertFromExcelRef() (see https://rdrr.io/cran/openxlsx/man/convertFromExcelRef.html).
# The inverse function to openxlsx::int2col() (see https://rdrr.io/cran/openxlsx/man/int2col.html).
#
# excelColsToInt(c('a','F','x','aa','az','aaa','a','Az','aZ','AZ'))
# #=> int [1:10] 1 6 24 27 52 703 1 52 52 52
#
# x <- c('a','F','x','aa','az','aaa','a','Az','aZ','AZ')
# n <- excelColsToInt(x)
# x2 <- openxlsx::int2col(n)
@alekrutkowski
alekrutkowski / parse_Oracle_quick_DDL_sql.R
Last active July 16, 2021 08:42
Parse and visualise Oracle metadata SQL file obtained via "Quick DDL > Save to File..." in R
# Parse Oracle metadata SQL file obtained via "Quick DDL > Save to File..."
# (see https://stackoverflow.com/a/14262027)
# in Oracle SQL Developer (tested with version 19.2.1.247.2212)
# into R code which builds a list of data.tables with 0 length columns
# which can be later visualised via autoschema::schema -- see
# the example:
# r_code <- parseQDDL('exported.sql') ## the top-level function defined below
# library(autoschema) ## it can be obtained from https://github.com/alekrutkowski/autoschema
# schema(eval(parse(text=r_code))) ## see the graph or...
@alekrutkowski
alekrutkowski / data.table_summaries.R
Created January 10, 2022 08:34
R data.table: Summarize each column with each function and name the new columns automatically
library(magrittr)
library(data.table)
summaries <- function(dt, col_names, fun_names, additional_code, by, sep='__')
eval(parse(
text=
expand.grid(var=col_names, fun=fun_names) %>%
{paste0('`',.$fun,sep,.$var,'`=`',.$fun,'`(',.$var,')')} %>%
c(additional_code) %>%
paste(collapse=',') %>%
@alekrutkowski
alekrutkowski / eurostat_extended.py
Created May 18, 2022 09:49
Additional functions to `eurostat` Python package (https://pypi.org/project/eurostat)
import eurostat
import pandas as pd
def importData(EurostatDatasetCode, flags=False):
"""
Import a dataset from Eurostat as a flat/melted table (pandas dataframe)
Parameter
----------
EurostatDatasetCode : str
@alekrutkowski
alekrutkowski / panel.data.table.R
Last active June 2, 2022 13:33
R functions for panel data extending packages `data.table` and `collapse`
# ## Example: ⮦ time=3 missing
# library(magrittr)
# data.table(time = c(1,2, 4,5,6,
# 1,2),
# my_x = c(11:15,
# 11,12),
# my_y = c(101,103,105,NA,109,
# 111,121),
# group = c(rep.int('a',5),
# rep.int('b',2))) %>%
@alekrutkowski
alekrutkowski / eurodata.extras.R
Created May 31, 2022 13:45
Additional functions extending the R package `eurodata`
library(magrittr)
monthToQuarter <- function(charvec, safe=TRUE) {
# monthToQuarter(c('2022M01','2022M02','2022M03','2022M04',
# NA_character_,'2022M12'))
# # "2022Q1" "2022Q1" "2022Q1" "2022Q2" NA "2022Q4"
if (safe)
stopifnot(all(grepl('^[1-2][0-9]{3}M[0-1][0-9]',
charvec) | is.na(charvec)))
yr <-
@alekrutkowski
alekrutkowski / import_AMECO.R
Last active October 5, 2022 15:18
R script to import European Commission's (DG ECFIN's) Annual Macro-ECOnomic database (AMECO)
library(magrittr)
library(data.table)
download.file('https://ec.europa.eu/economy_finance/db_indicators/ameco/documents/ameco0.zip',
'ameco0.zip')
time_stamp <-
'https://ec.europa.eu/info/business-economy-euro/indicators-statistics/economic-databases/macro-economic-database-ameco/download-annual-data-set-macro-economic-database-ameco_en' %>%
rvest::read_html() %>%
rvest::html_elements(xpath='//*[@id="block-ewcms-theme-main-page-content"]/article/div/div/div/div[2]/div/div/p[1]/text()[2]') %>%
rvest::html_text() %>%
@alekrutkowski
alekrutkowski / .Rprofile
Last active November 17, 2022 16:31
Functions and active bindings for rapid interactive work in R
options(width=200)
if (interactive()) {
message('================================================================================')
ab <- function(sym_as_str, fun)
makeActiveBinding(sym_as_str, fun, .GlobalEnv)
library(magrittr)
message('- Imported package `magrittr`')
library(data.table)
@alekrutkowski
alekrutkowski / multiple_dispatch.hs
Last active August 17, 2022 19:16
Multiple dispatch (double dispatch) in Haskell
main = do
putStrLn $ show $ add (MyInt 1) (MyInt 3)
-- MyInt 4
putStrLn $ show $ add (MyInt 13) (MyString "B")
-- MyString "13B"
putStrLn $ show $ add (MyString "B") (MyInt 13)
-- MyString "B13"
putStrLn $ show $ add (MyString "a") (MyString "b")
-- MyString "ab"
putStrLn $ show $ add' $ IntInt 1 3