Skip to content

Instantly share code, notes, and snippets.

View bfatemi's full-sized avatar
🎯
In my zone

Bobby Fatemi bfatemi

🎯
In my zone
View GitHub Profile
@bfatemi
bfatemi / matchArg.R
Created March 22, 2016 22:29
Example of match.arg()
require(stats)
## Extends the example for 'switch'
center <- function(x, type = c("mean", "median", "trimmed")) {
type <- match.arg(type)
switch(type,
mean = mean(x),
median = median(x),
trimmed = mean(x, trim = .1))
}
@reinholdsson
reinholdsson / merge_dt.r
Created July 11, 2014 12:58
R: merge multiple data tables
library(data.table)
x <- data.table(A = c(3,2,1), B = 4:6)
y <- data.table(A = 1:4, C = 5:8)
z <- data.table(A = 2:3, D = 5:6)
tbls <- list(x, y, z)
lapply(tbls, function(i) setkey(i, A))
merged <- Reduce(function(...) merge(..., all = T), tbls)