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
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>Audio Sprites</title> | |
| <meta type="description" content=""> | |
| <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> | |
| <style> | |
| * { | |
| margin: 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
| library(data.table) | |
| library(dplyr) | |
| library(tidyr) | |
| library(stringi) | |
| library(microbenchmark) | |
| set.seed(1) | |
| ndim <- 10000 | |
| ndate <- 1200 |
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
| fwf2csv <- function(infile) { | |
| if (infile == "clipboard") { | |
| infile <- tempfile() | |
| writeLines(overflow:::readClip(), infile) | |
| } | |
| a <- tempfile() | |
| text <- 'BEGIN{ FS=OFS=""; ARGV[ARGC]=ARGV[ARGC-1]; ARGC++ } | |
| NR==FNR { | |
| for (i=1;i<=NF;i++) { |
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
| half_my_life <- function(birth = "1978-09-05", event_date = "1997-05-30") { | |
| birth <- as.Date(birth) | |
| event_date <- as.Date(event_date) | |
| if (event_date <= birth) stop("You've got your dates mixed up....") | |
| Duration <- (event_date - birth) * 2 | |
| Date <- birth + (Duration) | |
| FDate <- format(Date, "%A %d %B %Y") | |
| Age <- as.numeric(Duration)/365.242 | |
| if (Date < Sys.Date()) { | |
| String <- c("You were able to say that this event has been half your life on ", |
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
| tableMaker <- function(invec) { | |
| ## http://stackoverflow.com/q/30528592/1270695 | |
| require(data.table) | |
| ## Split up the vector | |
| temp <- strsplit(invec, ",", TRUE) | |
| ## How long is each vector? | |
| a <- lengths(temp) | |
| ## Which vectors need adjustment? | |
| ind <- which(a %% 2 == 0) | |
| ## Adjust only those that need adjustment |
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
| dt <- data.table( | |
| Row = c(rep(1, 3), rep(2, 3), 3), | |
| Col = factor(c(1, 2, 8, 1, 2, 9, 2), 1:9), | |
| Value = c(31L, 56L, 13L, 83L,51L, 16L, 53L) | |
| ) | |
| str(dt) | |
| # Classes ‘data.table’ and 'data.frame': 7 obs. of 3 variables: | |
| # $ Row : num 1 1 1 2 2 2 3 | |
| # $ Col : Factor w/ 9 levels "1","2","3","4",..: 1 2 8 1 2 9 2 | |
| # $ Value: int 31 56 13 83 51 16 53 |
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
| contains <- function(pattern) { | |
| pattern <- pattern | |
| call_info <- deparse(sys.calls()[[1]]) | |
| if (grepl("(^.+\\()(.+)(\\)$)",call_info)) { | |
| this_name <- sub("(^.+\\()(.+)(\\)$)","\\2",call_info) | |
| } else { | |
| this_name <- strsplit(call_info,"\\[")[[1]][1] | |
| } | |
| this <- get(this_name) | |
| this_names <- names(this) |
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) | |
| n <- 100000 | |
| l <- cbind(sequence(n), sample(c(letters, LETTERS), n, TRUE)) | |
| l <- split(l, 1:nrow(l)) | |
| library(microbenchmark) | |
| library(stringi) | |
| rb <- function() do.call(rbind, l) | |
| rb2 <- function() { | |
| A <- data.frame(do.call(rbind, l), stringsAsFactors = 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
| dat1 <- data.frame(time = seq(10, 90, 10), | |
| value = c(NA, 0, 1, NA, NA, 30, 68, 0, NA)) | |
| dat2 <- data.frame(time = seq(10, 90, 10), | |
| value = c(0, NA, 1, NA, NA, 30, 68, 0, NA)) | |
| RStudent <- function(indf) { | |
| aux <- rle(as.numeric((!is.na(indf[, 2])))) | |
| cbind(TIME = indf[cumsum(aux$lengths)[which(aux$values == 1)] - | |
| aux$lengths[aux$values == 1] +1, 1], | |
| Result = rle(is.na(indf$value))$lengths[!rle(is.na(indf$value))$values]) |
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", fixed = TRUE, | |
| drop = TRUE, stripWhite = TRUE, type.convert = TRUE) { | |
| require(stringi) | |
| require(splitstackshape) | |
| Trim <- function(invec) { | |
| `dim<-`(stri_trim_both(invec), dim(invec)) | |
| } | |
| padNAcols <- function(inmat, colsneeded) { |