Skip to content

Instantly share code, notes, and snippets.

View alexland's full-sized avatar

doug ybarbo alexland

View GitHub Profile
@alexland
alexland / flatten.py
Last active December 24, 2015 17:29
flatten a nested sequence: distinguishes lists from strings, etc.
# first, a compact one-liner but limited to 2D sequences (nested just one level):
# obvious, but
# "nested_list" refers to the nested list you want to flatten,
# "row" refers to each list comprising the nested_list
# "itm" refers to each element comprising each row
def flatten(list2D):
return [itm for row in list2d for itm in row]
@alexland
alexland / exception_class_id.py
Created September 26, 2013 03:38
how to find the module an exception class was defined in
try:
...
except Exception, e:
print("caught an exception defined in class {0}".format(e.__class__.__module__))
@alexland
alexland / commify.R
Created July 3, 2013 22:45
add commas to numbers comprising various R objects (eg, tables)
print_fn = function(x) {print(format(x, big.mark=','), quote=F)}
@alexland
alexland / sort_tapply.R
Created October 10, 2012 02:13
sorting a data frame on a numeric variable conditioned on a factor variable
DF1 = DF[order(DF$factor_var, -xtfrm(DF$numeric_var)),]
DF1$idx = sequence(rle(as.character(DF1$factor_var))$lengths)
$> # e.g., sort a data frame based on 'price' (numeric variable)
$> # 'grouped by' the levels of 'color' (factor, or discrete variable)
$> data(diamonds, package="ggplot2")
@alexland
alexland / package-name-expander.R
Created October 8, 2012 06:02
for programatic update of an R package--avoid having to use complete filename with version appended
pkg_name = paste("~/path/to/package", system('cd /path/to/package; ls adStats*', intern=T), sep="/")
install.packages(pkg_name, repos=NULL, type="source")