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(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)) |
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
| 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 |
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
| 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 |
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(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) |
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
| #' 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 | |
| #' |
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(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)) |
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
| ## 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", |
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
| 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 |
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") |
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 |