Skip to content

Instantly share code, notes, and snippets.

View czeildi's full-sized avatar
🎲

Ildikó Czeller czeildi

🎲
View GitHub Profile
@czeildi
czeildi / week_numbers.R
Created August 28, 2017 08:46
how R handles week starts: Sunday vs Monday start
library("lubridate")
#>
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#>
#> date
a_sunday <- as.Date("2017-08-27")
# floor_date rounds to Sunday:
@czeildi
czeildi / dojo.py
Created August 22, 2017 13:21
python dojo skeleton with unittest
class Dojo:
def __init__(self, input):
self.input = input
def calculate(self):
return 42
@czeildi
czeildi / code_cov_for_project.R
Last active August 22, 2017 08:24
code coverage with covr for a project which is not an R package
# open Rproj with packrat
# restart R session
library("testthat")
source("libraries.R")
packrat::with_extlib(
c("rex", "covr", "DT"), {
library("rex")
library("covr")
@czeildi
czeildi / tennis_1_refactored.js
Created June 27, 2017 19:51
tennis refactoring kata solution
var TennisGame1 = function(player1Name, player2Name) {
this.scores = [
0,
0
];
this.players = [
player1Name,
player2Name
];
@czeildi
czeildi / dt_new_col.R
Created June 25, 2017 11:05
define new column from list of columns in data.table rowwise
# dt is a data.table with id_cols and value_cols
# we want rows where any of the value cols is not NA regardless of the
# values of the id_cols
dt[, to_keep := purrr::pmap_lgl(
purrr::map_df(
dt[, .SD, .SDcols = value_cols],
~ !is.na(.x)
),
any
@czeildi
czeildi / ts_aggregation.R
Created June 11, 2017 11:16
time series aggregation by different units in R using xts
library("xts")
dt <- data.table(
day = seq.Date(as.Date('2016-07-01'), by = 'day', length.out = 180),
x = 1:180,
y = 1001:1180
)
ts_data <- as.xts(dt)
@czeildi
czeildi / cumulative_uniqueN.R
Created June 11, 2017 10:28
compute cumulative unique N with R's data.table package
dt <- data.table(x = c(4,4,2,5,4,2,3))
dt[, g := .GRP, by = x]
dt[, cumuniquen := cummax(g)]