Skip to content

Instantly share code, notes, and snippets.

View mrdwab's full-sized avatar

Ananda Mahto mrdwab

View GitHub Profile
library(splitstackshape)
library(reshape2)
library(dplyr)
library(tidyr)
dfx <- data.frame(Var1=c("A", "B", "C", "D", "B", "C", "D", "C", "D", "D"),
Var2=c("E", "E", "E", "E", "A", "A", "A", "B", "B", "C"),
Var1out = c(1,-1,-1,-1,1,-1,-1,1,-1,-1),
Var2out= c(-1,1,1,1,-1,1,1,-1,1,1))
@mrdwab
mrdwab / helpExtract.R
Last active August 29, 2015 14:07
Modified version of helpExtract
helpExtract <- function(Function, section = "Usage", type = "m_code", sectionHead = NULL) {
A <- deparse(substitute(Function))
x <- capture.output(tools:::Rd2txt(utils:::.getHelpFile(utils::help(A)),
options = list(sectionIndent = 0)))
B <- grep("^_", x) ## section start lines
x <- gsub("_\b", "", x, fixed = TRUE) ## remove "_\b"
X <- rep(FALSE, length(x)) ## Create a FALSE vector
X[B] <- 1 ## Initialize
out <- split(x, cumsum(X)) ## Create a list of sections
sectionID <- vapply(out, function(x) ## Identify where the section starts
set.seed(1)
matches <- replicate(1000000, sample(20, sample(20), TRUE))
fun1 <- function() unlist(lapply(matches, sum))
fun2 <- function() sapply(matches, sum)
fun3 <- function() vapply(matches, sum, numeric(1L))
fun4 <- function() rapply(matches, sum)
library(microbenchmark)
microbenchmark(L = fun1(), S = fun2(), V = fun3(), R = fun4(), times = 10)
# Unit: seconds
# expr min lq mean median uq max neval
library(data.table)
library(dplyr)
x <- read.csv(textConnection(
'0,1,1,0
0,1,1,0
1,0,1,0
0,1,0,1
1,0,0,1'),
header = FALSE)
@mrdwab
mrdwab / binMatFuns.R
Last active August 29, 2015 14:07
Convert binary matrices by row to integer values (and related functions)
#' Convert a binary matrix to a vector of integers (by row)
#'
#' The \code{mat2int} function converts a binary matrix to a vector of integers.
#'
#' @param inmat The input matrix.
#' @return A vector of integers.
#' @author Ananda Mahto
#' @seealso \code{\link{strtoi}}.
#' @examples
#'
@mrdwab
mrdwab / stratified_examples.R
Created September 29, 2014 10:13
Examples for the `stratified` function.
# library(devtools)
# source_gist("https://gist.github.com/mrdwab/6424112")
# Generate a couple of sample data.frames to play with
set.seed(1)
dat1 <- data.frame(ID = 1:100, A = sample(c("AA", "BB", "CC", "DD", "EE"), 100,
replace = TRUE), B = rnorm(100), C = abs(round(rnorm(100), digits = 1)),
D = sample(c("CA", "NY", "TX"), 100, replace = TRUE), E = sample(c("M",
"F"), 100, replace = TRUE))
## Required packages
library(data.table)
library(dplyr)
library(reshape2)
library(microbenchmark)
## Small sample `data.frame`
dat <- structure(list(Pos = c("Position1", "Position2", "Position3",
"Position1", "Position3", "Position2"), M = c(34L, 45L, 89L,
56L, 54L, 56L), P = c(56L, 23L, 78L, 45L, 35L, 89L)), .Names = c("Pos",
df <- structure(list(id = 1:11, date = c("2013-01-08", "2013-01-09",
"2013-01-10", "2013-01-11", "2013-01-12", "2013-01-13", "2013-01-14",
"2013-01-15", "2013-01-16", "2013-01-17", "2013-01-18"), Events = structure(c(7L,
2L, 1L, 3L, 10L, 8L, 9L, 11L, 4L, 6L, 5L), .Label = c("", "Fog",
"Fog-Rain", "Fog-Rain-Thunderstorm", "Fog-Rain-Thunderstorm-Snow",
"Fog-Thunderstorm", "Rain", "Rain-Snow", "Rain-Thunderstorm",
"Snow", "Thunderstorm"), class = "factor")), .Names = c("id",
"date", "Events"), row.names = c(NA, -11L), class = "data.frame")
df
@mrdwab
mrdwab / stratifiedDT.R
Last active April 10, 2019 06:29
Attempt to rewrite stratified for `data.table`. The `data.frame` version can be found at https://gist.github.com/mrdwab/6424112
stratifiedDT <- function(indt, group, size, select = NULL,
replace = FALSE, keep.rownames = FALSE,
bothSets = FALSE) {
if (is.numeric(group)) group <- names(indt)[group]
if (!is.data.table(indt)) indt <- as.data.table(
indt, keep.rownames = keep.rownames)
if (is.null(select)) {
indt <- indt
} else {
if (is.null(names(select))) stop("'select' must be a named list")
@mrdwab
mrdwab / pdfWatermarkEncrypt.bat
Created August 8, 2014 10:33
Use PDFtk to add a "stamp" or a "background" to a PDF and restrict editing/copying.
@echo off
if [%1]==[] goto :eof
:loop
pdftk %1 stamp back.pdf output "%~dpn1_new%~x1" owner_pw somepasswordyouwant
shift
if not [%1]==[] goto loop