This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) %>% |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Use Gists to store code you would like to remember later on | |
| console.log(window); // log the "window" object to the console |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## source: https://twitter.com/eddelbuettel/status/607675836779642880 | |
| quietLibrary <- function(x, ...) eval(substitute(suppressMessages(library(x, ...)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # --------------------------------------- | |
| # 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. | |
| #' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # --------------------------------------- | |
| # 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## 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 |
OlderNewer