Skip to content

Instantly share code, notes, and snippets.

View robertzk's full-sized avatar

Robert Krzyzanowski robertzk

View GitHub Profile
@robertzk
robertzk / randomreplace.r
Created December 6, 2013 00:36
R exercise: Create a replacement function that modifies a random location in vector. http://adv-r.had.co.nz/Functions.html
"random<-" <- function(object, position, value) {
object[sample(seq_along(object), 1)] <- value
object
}
# Example:
# > x <- c(1,2,3,4,5)
# > random(x) <- 10
# > x
# [1] 1 10 3 4 5
@robertzk
robertzk / infixed.r
Created December 6, 2013 00:33
R exercise: Create infix versions of set functions: intersect(), union(), setdiff() http://adv-r.had.co.nz/Functions.html
infixed_names <- list(intersect = '∩', union = 'U', setdiff = '-')
create_infix_operator <- function(name, base_fn, e) {
name <- paste("%", name, "%", sep = '')
assign(name, function(x, y) get(base_fn, envir = e)(x, y), envir = e)
}
lapply(names(infixed_names), function(name) {
create_infix_operator(infixed_names[[name]], name, e = parent.frame(2))
})
@robertzk
robertzk / gist:7816384
Created December 6, 2013 00:01
R exercise: http://adv-r.had.co.nz/Functions.html Create a list of all the replacement functions found in the base package. Which ones are primitive functions?
x <- ls("package:base")
x[substring(x, nchar(x)-1, nchar(x)) == '<-']
# "[[<-" "[<-" "@<-" "<-" "<<-" "$<-" "attr<-" "attributes<-" "body<-" "class<-" "colnames<-" "comment<-" "diag<-" "dim<-"
# "dimnames<-" "Encoding<-" "environment<-" "formals<-" "is.na<-" "length<-" "levels<-" "mode<-" "mostattributes<-" "names<-" "oldClass<-" "parent.env<-" "regmatches<-" "row.names<-"
# "rownames<-" "split<-" "storage.mode<-" "substr<-" "substring<-" "units<-"
`||` <- function(a,b) if(is.null(a)) b else base::`||`(a,b)
@robertzk
robertzk / xor.r
Created December 6, 2013 00:05
R exercise: Create an infix xor() operator. http://adv-r.had.co.nz/Functions.html
`%xor%` <- function(x,y) (x || y) && !(x && y)
@robertzk
robertzk / error_handling.R
Created November 2, 2015 23:11
Capture error handling in R
ref_filename <- function(ref) {
attr(ref, "srcfile")$filename
}
frame_text <- function(frame) {
if (identical(frame, .GlobalEnv)) {
structure(pkg = "_global",
"global environment"
)
@robertzk
robertzk / s3subset.R
Created September 2, 2015 22:31
S3 subsetting overloading
wrapped <- function(obj) {
structure(obj, class = "wrapped")
}
`[[.wrapped` <- function(obj, x) {
if (x == "predict") {
.subset2(obj, x)
} else {
.subset2(obj, "model")[[x]]
}
@robertzk
robertzk / Setting-Up-Hipchat-Token.md
Last active August 29, 2015 14:27
Setting up your hipchat token.

Setting up your Hipchat API token

First, visit the Hipchat account API page. You will have to login with you avantcredit.com hipchat account.

Scroll down and create your Hipchat token with the following permissions:

Create New Token

The token should appear in the list above. Copy the token, and then run the following line from your terminal (with the token replaced by your token):

@robertzk
robertzk / send.vim
Created July 26, 2015 14:12
vim send macro
let @s='^i! git add "$(git rev-parse --show-toplevel)"; git commit -m "^[$a"; git push -q origin `git rev-parse --abbrev-ref HEAD` &^[dd;w^M;@"^M^M^['
@robertzk
robertzk / Syberia-FAQ
Created May 31, 2015 15:28
Initial syberia FAQ
Can I use CSV files? What about streaming production data? So I have this SAS file...
You can use any kind of data ingest your heart desires. The built-in import stage comes with support for many common formats (link), but it is easy to add more (link).
If you wish to use your live production data, write a package and add an import adapter (link).
What is a mungebit and why are you making up words?
A mungebit is the correct mathematical abstraction for wrangling a data set in a way that you won't have to bug a software or data engineer to make it "production ready" or live in "the data pipeline." It means you can turn the 90% of time data scientists spend on data wrangling into 10%.