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
DT <- as.data.table(iris)
DT[, MONTHS := sample(month.abb, size = .N, replace = TRUE)]
DT[, LETTERS := sample(LETTERS[rep(1:5, 3)], size = .N, replace = TRUE)]
DT
grp1 <- c("Species", "LETTERS")
grp2 <- c("MONTHS")
resDT <- DT[, .(grpDT = {
res <- list(as.data.table(.SD)[, .N, grp2])
res
@bfatemi
bfatemi / init_script.R
Last active August 12, 2017 04:57
Launch New Package
# DEFINE HELPER FUNS AND SET INIT OPTIONS ---------------------------------
options(devtools.name = "Bobby Fatemi")
options(devtools.desc.license = "file LICENSE")
options(devtools.desc.author = 'person(email = "[email protected]", role = c("aut", "cre"), given = "Bobby", family = "Fatemi")')
options(devtools.desc = list(
Description = "[DOCUMENTATION NEEDED] DESCRIPTION.",
Title = "[DOCUMENTATION NEEDED] TITLE")
)
packageStartupMessage("
##********* Surgical Science **********##
# -------------------------------- #
# Medical Outcomes Analysis #
# -------------------------------- #
# Comparison of daVinci Robotic #
# Surgery Patients and Patients #
# of other surgical modalities #
# -------------------------------- #
# #
@bfatemi
bfatemi / RemoveNAallCols
Created April 29, 2016 00:44
One line to keep rows such that there are no NAs across all columns
# One line to keep rows such that there are no NAs across all columns
dt[Reduce("&", lapply(dt, function(i) !is.na(i)))]
@bfatemi
bfatemi / Namemasking.R
Last active April 11, 2016 17:47
Name masking - Preserving definition environment
j <- function(x) {
y <- 2 # Define y
# j returns this function
function() {
c(x, y)
}
}
@bfatemi
bfatemi / getDays.R
Created March 27, 2016 20:16
Get a vector containing the sequence of days beginning with today, and ending with year end.
today <- Sys.Date()
yearEnd <- as.Date(paste0(year(today), "-12-31"))
daysVec <- seq(today, yearEnd, by=1)
@bfatemi
bfatemi / accessFnStack.R
Created March 23, 2016 17:45
Example of accessing the function call stack
### Example of accessing the function call stack
IsHungry <- function(){
e <- substitute(cat(sub("\\(.*\\)", " ", deparse(sys.calls()[[i]]))))
Bob <- function(n){
Loves <- function(n){
Burgers <- function(n){
TheEnd <- function(n){
invisible(sapply(1:n, function(i) eval(e, list(i))))
}
TheEnd(sys.nframe())
@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))
}
@bfatemi
bfatemi / getSizeandDepends.sh
Created March 16, 2016 05:11
Snippet to get the dependencies and size of those dependencies for any package install
#!/bin/bash
LIST=$1
PACKAGE_LIST=$(dpkg --get-selections | awk '{ print $1 }' | grep -v -e "-dbg" | cut -f1 -d":")
getsize () {
size=$(apt-cache --no-all-versions show $1 | grep Installed-Size | awk '{ print $2 }')
((NEEDED+=$size))
}
@bfatemi
bfatemi / dowhileactive.sh
Created March 16, 2016 05:09
Snippet to do something while a particular process is active
apt-get update |pv -tN "Updating" > /dev/null & pid=$!
trap "kill $pid 2> /dev/null" EXIT
while kill -0 $pid 2> /dev/null; do
tput cup 10 10
done
trap - EXIT