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
TrueSeq <- function(inLogi, zero2NA = FALSE) { | |
x <- rle(cumsum(!inLogi)[inLogi])$lengths | |
inLogi[inLogi] <- rep(seq_along(x), x) | |
if (isTRUE(zero2NA)) inLogi[inLogi == 0] <- NA | |
inLogi | |
} |
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
#' Interleaves values within matrices or vectors | |
#' | |
#' Mimics some of the behavior of the \code{Riffle} function | |
#' (\url{http://reference.wolfram.com/mathematica/ref/Riffle.html}) in | |
#' Mathematica. For matrices, it interleaves the columns. For vectors, it | |
#' interleaves differently according to whether the subsequent values are | |
#' presented as separate values or whether they are grouped with \code{c()}. | |
#' | |
#' It is expected that all matrices to be interleaved would have the same | |
#' number of rows, though they may have differing numbers of columns. If they |
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
#' Find a needle in a haystack... | |
#' | |
#' Find specified search patterns (in any order, not necessarily joined) in another vector of strings. | |
#' | |
#' @param findMe What are you looking for? A character vector. | |
#' @param findIn Where are you looking for it? A character vector. | |
#' @return A matrix with 1 indicating presence and 0 indicating absence. | |
#' @author Ananda Mahto | |
#' @references \url{http://stackoverflow.com/q/22129542/1270695} | |
#' @examples |
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
dcastMult <- function(data, formula, value.var = "value", | |
funs = list("min" = min, "max" = max)) { | |
require(reshape2) | |
if (is.null(names(funs)) | any(names(funs) == "")) stop("funs must be named") | |
Form <- formula(formula) | |
LHS <- as.character(Form[[2]]) | |
if (length(LHS) > 1) LHS <- LHS[-1] | |
temp <- lapply(seq_along(funs), function(Z) { | |
T1 <- dcast(data, Form, value.var = value.var, | |
fun.aggregate=match.fun(funs[[Z]]), fill = 0) |
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
cSplit <- function(indt, splitCols, sep = ",", direction = "wide", | |
makeEqual = NULL, fixed = TRUE, drop = TRUE, | |
stripWhite = FALSE) { | |
message("`cSplit` is now part of the 'splitstackshape' package (V1.4.0)") | |
## requires data.table >= 1.8.11 | |
require(data.table) | |
if (!is.data.table(indt)) setDT(indt) | |
if (is.numeric(splitCols)) splitCols <- names(indt)[splitCols] | |
if (any(!vapply(indt[, splitCols, with = FALSE], | |
is.character, logical(1L)))) { |
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
L <- c("1 1:1 2:1 3:1 5:1 6:1 8:1", | |
"5 1:1 2:1 4:1", | |
"9 1:1 2:1 7:1 10:1") | |
M <- replicate(10000, L) | |
M2 <- strsplit(M, "\\s+|:") | |
head(numMat(lapply(M2, `[`, -1))) | |
# 1 2 3 4 5 6 7 8 9 10 | |
# [1,] 1 1 1 0 1 1 0 1 0 0 | |
# [2,] 1 1 0 1 0 0 0 0 0 0 |
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
Col1 <- c("a", "b","b",NA) | |
Col2 <- c(NA, "a", "c", NA) | |
Col3 <- c(NA,NA,"b", "a") | |
dat <- data.frame(Col1, Col2, Col3) | |
fun1 <- function() { | |
cbind(dat, | |
apply(table(cbind(rn = 1:nrow(dat), | |
stack(lapply(dat, as.character)))), | |
c(1, 2), sum)) |
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
REM ***** BASIC ***** | |
Function CELL_NOTE(vSheet,lRowIndex&,iColIndex%) | |
Dim v | |
v = getSheetCell(vSheet,lRowIndex&,iColIndex%) | |
if vartype(v) = 9 then | |
CELL_NOTE = v.Annotation.getText.getString | |
else | |
CELL_NOTE = v | |
endif | |
End Function |
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
@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 |
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
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") |