Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View mrdwab's full-sized avatar

Ananda Mahto mrdwab

View GitHub Profile
@mrdwab
mrdwab / cSplit_e.R
Created March 8, 2018 17:08
Faster versions of `cSplit_e`, `numMat`, and `charMat`.
cSplit_e_new <- function (indt, splitCols, sep = ",", mode = "binary", type = "numeric",
drop = FALSE, fixed = TRUE, fill = NULL) {
indt <- setDT(copy(indt))
if (is.numeric(splitCols)) splitCols <- names(indt)[splitCols]
if (length(sep) == 1) sep <- rep(sep, length(splitCols))
if (length(sep) != length(splitCols)) stop("Wrong number of sep supplied")
if (length(mode) == 1) mode <- rep(mode, length(splitCols))
if (length(mode) != length(mode)) stop("Wrong number of mode supplied")
if (any(!mode %in% c("binary", "value", "count"))) {
@mrdwab
mrdwab / trim_ws.R
Last active March 8, 2018 17:07
Fast leading and trailing whitespace trimming for lists and vectors. Preserves `NA` values.
trim_list <- function(x, relist = TRUE, convert = FALSE) {
x <- replace(x, lengths(x) == 0, NA_character_)
y <- unlist(x, use.names = FALSE)
y[!nzchar(y)] <- NA_character_
out <- trim_vec(y, TRUE)
if ((attr(out, "test") == "clean") & (!isTRUE(convert))) x
if (isTRUE(convert)) out <- type.convert(out, as.is = TRUE)
if (isTRUE(relist)) {
out <- split(out, factor(rep.int(seq.int(length(x)), lengths(x))))
if (is.null(names(x))) unname(out) else `names<-`(out, names(x))
@mrdwab
mrdwab / cSplit_fread_tests.R
Last active March 10, 2018 11:39
Using `fread` as the workhorse for `cSplit`.
dt1 <- fread("V1 V2 V3
x b;c;d 1
y d;ef 2
z d;ef 3
m tmp 4
n tmp 5
n x;yz 5")
dt1[4, V2:=''] # this record will be lost because V2 value is empty string
dt1[5, V2:=NA_character_] # NA value is processed correctly
@mrdwab
mrdwab / MELT.R
Last active March 3, 2018 16:16
Possible replacement for `merged.stack`. Need to figure out how to incorporate sep in here too.... See http://stackoverflow.com/a/34427860/1270695
# Should be faster than the other option here, hopefully with not too much overhead compared to `melt`
NA_type <- function(string) {
switch(string,
double = NA_real_,
integer = NA_integer_,
complex = NA_complex_,
character = NA_character_,
NA)
}
@slhck
slhck / html5-video-presentation.html
Last active May 17, 2023 02:35
html5-video-presentation
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<style>
video {
width: 100%;
height: auto;
}
</style>
</head>
@mattsigal
mattsigal / stratifiedNorm.R
Last active August 29, 2015 14:11
Create Stratified Norm Sample from Strata and Count Vector
#' Function to draw observations from stratified norm sample
#'
#' This function creates a stratified random sample, given an arbitrary number of
#' factors and levels within those factors.
#'
#' @param dat data.frame object
#' @param strata character; a named vector indicating the strata variables
#' @param observations numeric; number of cases to sample from each strata
#' @param return.grid logical; return grid of factor combintions?
#' @param full.data logical; return the original dataset with a new logical