Skip to content

Instantly share code, notes, and snippets.

View BERENZ's full-sized avatar

Maciej Beręsewicz BERENZ

View GitHub Profile
@BERENZ
BERENZ / Anscombe_transmute.R
Created March 23, 2015 09:56
Anscombe - przetworzenie na zajecia
library(dplyr)
library(tidyr)
library(ggplot2)
anscombe_transmuted <- anscombe %>%
gather(x,val,x1:x4) %>%
gather(y,val2,y1:y4) %>%
mutate(OX = extract_numeric(x),
OY = extract_numeric(y)) %>%
filter(OX == OY) %>%
@BERENZ
BERENZ / gist:aee5cda46289c3407742
Last active August 29, 2015 14:17
Liczba zebranych głosów
library(ggplot2)
d <- data.frame(Kandydat = c('Komorowski','Duda','Ogórek','Korwin-Mikke',
'Kukiz','Jarubas','Kowalski','Palikot','Wilk',
'Tanajno','Braun','Grodzka','Nowicka'),
Poparcie = c(650,1600,510,300,240,450,188,150,145,130,125,85,91))
ggplot(data = d,
aes(x = reorder(Kandydat,-Poparcie),
@BERENZ
BERENZ / 0_reuse_code.js
Last active August 29, 2015 14:19
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@BERENZ
BERENZ / recode.R
Last active August 29, 2015 14:22 — forked from hadley/recode.R
recode <- function(df, ..., match = c("first", "last")) {
match <- match.arg(match)
cases <- lapply(list(...), as.case)
if (identical(match, "last")) cases <- rev(cases)
n <- nrow(df)
out <- rep(NA, length(n)) # logical will be upcast as needed
# Simple loop-y implementation
@BERENZ
BERENZ / quietLibrary
Created June 8, 2015 07:07
quietLibrary
## source: https://twitter.com/eddelbuettel/status/607675836779642880
quietLibrary <- function(x, ...) eval(substitute(suppressMessages(library(x, ...))))
@BERENZ
BERENZ / fast_dummy
Created July 8, 2015 12:28
fast dummy
## source http://stackoverflow.com/questions/23035982/directly-creating-dummy-variable-set-in-a-sparse-matrix-in-r
fast_dummy <- function(df) {
n <- nrow(df)
nlevels <- sapply(df, nlevels)
i <- rep(seq_len(n), ncol(df))
j <- unlist(lapply(df, as.integer)) +
rep(cumsum(c(0, head(nlevels, -1))), each = n)
x <- 1
mat <- sparseMatrix(i = i, j = j, x = x)
return(mat)
@BERENZ
BERENZ / calibVars.r
Created July 8, 2015 12:29
Calib vars with sparse Matrix
# ---------------------------------------
# Author: Andreas Alfons
# Vienna University of Technology
# ---------------------------------------
#' Construct a matrix of binary variables for calibration
#'
#' Construct a matrix of binary variables for calibration of sample weights
#' according to known marginal population totals.
#'
@BERENZ
BERENZ / calibWeights.r
Created July 8, 2015 12:29
Calib weights with sparse Matrix
# ---------------------------------------
# Author: Andreas Alfons
# Vienna University of Technology
# ---------------------------------------
#' Calibrate sample weights
#'
#' Calibrate sample weights according to known marginal population totals.
#' Based on initial sample weights, the so-called \emph{g}-weights are computed
#' by generalized raking procedures.
# improved list of objects
# source: http://stackoverflow.com/questions/1358003/tricks-to-manage-the-available-memory-in-an-r-session/4827843#4827843
.ls.objects <- function (pos = 1, pattern, order.by,
decreasing=FALSE, head=FALSE, n=5) {
napply <- function(names, fn) sapply(names, function(x)
fn(get(x, pos = pos)))
names <- ls(pos = pos, pattern = pattern)
obj.class <- napply(names, function(x) as.character(class(x))[1])
obj.mode <- napply(names, mode)
obj.type <- ifelse(is.na(obj.class), obj.mode, obj.class)
@BERENZ
BERENZ / mclapply2.R
Created October 25, 2015 14:43
mclapply with progress bar
## Source: https://stackoverflow.com/questions/10984556/is-there-way-to-track-progress-on-a-mclapply/26892969#26892969
library(parallel)
##------------------------------------------------------------------------------
##' Wrapper around mclapply to track progress
##'
##' Based on http://stackoverflow.com/questions/10984556
##'
##' @param X a vector (atomic or list) or an expressions vector. Other
##' objects (including classed objects) will be coerced by