Skip to content

Instantly share code, notes, and snippets.

View daroczig's full-sized avatar

Gergely Daróczi daroczig

View GitHub Profile
@daroczig
daroczig / exercises.R
Last active April 5, 2018 08:24
data.table joins
## generate transactional tables
set.seed(42)
library(data.table)
tx <- data.table(
item = sample(letters[1:3], 10, replace = TRUE),
time = as.POSIXct(as.Date('2016-01-01')) - runif(10) * 36*60^2,
amount = rpois(10, 25))
prices <- data.table(
item = letters[1:3],
date = as.Date('2016-01-01') - 1:2,
## intro slides: http://bit.ly/r-intro-slide
## basic operations
1 + 3
3*2
3^2
## constants
pi
"pi"
@daroczig
daroczig / CRUNCH-2017-R-workshop.R
Created October 19, 2017 12:28
Code presented at the R workshop of the CRUNCH 2017 conference: http://crunchconf.com
## intro slides: http://bit.ly/CRUNCH-R-2017
## basic operations
1 + 3
3*2
3^2
## constants
pi
"pi"
@daroczig
daroczig / data.table.R
Created April 12, 2017 23:11
overlap join demo
# demo transactions data
library(data.table)
txns <- data.table(
rpid = rep(1:3, times = 4),
txid = 1:12,
time = c(10, 10, 10, 11, 15, 20, 12, 16, 25, 13, 21, 30))
## overlap join to see which transactions happened withing 3 time units on the same rpid
## let's define the time periods for the overlap
txns[, start := time - 3]
@daroczig
daroczig / article.Rmd
Last active November 9, 2016 14:39
Daróczi Gergely (2016): Alkalmazott statisztika? R! In. Statisztikai Szemle. KSH.
---
title: Alkalmazott statisztika? R!
author: Daróczi Gergely
date: 2016
---
Az R programozási nyelvvel és adatelemző, statisztikai és adatvizualizációs rendszerrel (R Core Team 2016) kicsit több mint 10 éve ismerkedtem meg felsőfokú tanulmányaim során, amikor is egy választható gazdaságszociológia kurzus keretén belül a magyarországi burgonyapiac kaotikus viselkedésével (Vizvári, Bacsi 1997; Vizvári 2002) volt szerencsém rövidebben foglalkozni. Ezt a személyes emléket azért tartottam fontosnak leírni, mert a káoszelmélettel való ismerkedés in medias res -- a kapcsolódó meglehetősen összetettnek tűnő matematikai háttér tárgyalása nélkül --, az alkalmazással indult, és az R-nek köszönhetően a félév végén sikerrel abszolváltam a kurzust. Ezzel párhuzamosan egy új és igen gazdag világ tárult fel előttem az R eszköztárával, amely évekkel később egyik legkedvesebb szabadidős elfoglaltságommá, majd elsődleges munkaeszközömmé vált.
Noha az R nyelv már több mint 20 éves múltra tekinthet vissza, népszerűségét legin
@daroczig
daroczig / apply stuff on numeric cols.R
Last active April 29, 2016 22:48
apply stuff on numeric cols
ddist <- function(df, quantiles = c(0,.02, .25, .50, .75, .90, .98, .99, .999, 1), na.rm = TRUE) {
numvars <- which(sapply(df, is.numeric))
sapply(numvars, function(v) {
if (is.data.table(df)) {
v <- df[, v, with = FALSE]
} else {
v <- df[, v]
}
c(n = length(v),
ndistinct = length(unique(v)),
@daroczig
daroczig / memisc_and_pander.R
Last active January 24, 2018 19:22
Multiple regression models in a markdown table with memisc and pander
m1 <- lm(mpg ~ hp, data = mtcars)
m2 <- lm(mpg ~ hp + drat, data = mtcars)
m3 <- lm(mpg ~ hp + drat + factor(gear), data = mtcars)
library(pander); library(memisc)
panderOptions('table.alignment.rownames', 'left')
pander(relabel(
mtable(m1, m2, m3,
summary.stats=c('N', 'R-squared', 'F')),
'(Intercept)' = 'Constant',
@daroczig
daroczig / update-col-in-Redshift.sql
Created March 23, 2016 00:01
Update column definitions in Redshift
-- create a new temp table with exact same schema
CREATE TABLE foobar_temp (LIKE foobar INCLUDING DEFAULTS);
-- or create this temp table with any new schema (eg updated col type)
CREATE TABLE foobar_temp (...)
-- copy everything from old table
INSERT INTO foobar_temp <list of columns> SELECT <list of columns> FROM foobar;
-- rename/drop tables
## fetch & parse data from Wikipedia
library(XML)
wiki <- 'https://en.wikipedia.org/wiki/Chronology_of_computation_of_%CF%80'
tables <- readHTMLTable(readLines(wiki), stringsAsFactors = FALSE)
## merge data from 4 tables
library(data.table)
pis <- rbindlist(list(
## data data before 1400 from 3rd table extracted by hand
@daroczig
daroczig / get-data.R
Last active April 4, 2024 20:23
Number of R packages submitted to CRAN
## original idea & report by Henrik Bengtsson at
## https://stat.ethz.ch/pipermail/r-devel/2016-February/072388.html
## This script downloads the list of currently published R packages
## from CRAN and also looks at all the archived package versions to
## combine these into a list of all R packages ever published on
## CRAN with the date of first release.
## CRAN mirror to use
CRAN_page <- function(...) {