Skip to content

Instantly share code, notes, and snippets.

View mrdwab's full-sized avatar

Ananda Mahto mrdwab

View GitHub Profile
<!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;
library(data.table)
library(dplyr)
library(tidyr)
library(stringi)
library(microbenchmark)
set.seed(1)
ndim <- 10000
ndate <- 1200
@mrdwab
mrdwab / fwf2csv.R
Created June 26, 2015 02:28
Use awk to help convert fixed width to csv. Not tested extensively. Works based on stacks of spaces at common positions.
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++) {
@mrdwab
mrdwab / server.R
Created June 1, 2015 03:09
Half my life....
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 ",
@mrdwab
mrdwab / tableMaker.R
Last active August 29, 2015 14:22
SO30528592
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
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
@mrdwab
mrdwab / selectR.R
Created May 12, 2015 17:01
Used to select columns in a data.table according to the column names.
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)
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)
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])
@mrdwab
mrdwab / cSplit.R
Last active August 29, 2015 14:08
Even faster version of cSplit....
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) {