Skip to content

Instantly share code, notes, and snippets.

View intro_to_R
######### Intro to R ###############
######### The Data Frame ###########
df <- data.frame(
row.names = c('HaLikud', 'Yesh Atid', 'HaAvoda', 'HaBait HaYehudi', 'Yehadut HaTora', 'Meretz', 'Shas'),
LeaderName = c('Netanyahu', 'Lapid', 'Yehimovitch', 'Bennet', 'Litzman', 'GalOn', 'Yishai'),
Category = c('Right', 'Center', 'Left', 'Right', 'Religious', 'Left', 'Religious'),
Mandates = c(31, 19, 15, 12, 7, 6, 11)
)
@Nimster
Nimster / ringbuffer.rb
Created Nov 15, 2012 — forked from eerohele/ringbuffer.rb
A simple ring buffer for Ruby.
View ringbuffer.rb
class RingBuffer < Array
attr_reader :max_size
def initialize(max_size, enum = nil)
@max_size = max_size
enum.each { |e| self << e } if enum
end
def <<(el)
if self.size < @max_size || @max_size.nil?
@Nimster
Nimster / gist:3683049
Created Sep 9, 2012
Liam Gibbing's Python Cheat Sheet
View gist:3683049
This is taken from Liam Gibbing's Python Cheat Sheet at https://docs.google.com/file/d/0B9VT_L2CDnKvODYyNTc5NjktYmMyOC00NDFkLTliNTctMzQzMTAzYjUyYmYy/view?pli=1&sle=true
I just don't like the DocX format so I imported it here.
FUNCTIONS, METHODS & ATTRIBUTES
NUMBERS
• math.pi and math.e: Returns values of both pi and exponential const. respectively.
• math.sqrt(integer1): Returns square root of integer, use cmath.sqrt for negatives.
• math.floor(integer1): Returns integer rounded down to nearest whole.
• math.ceil(integer1): Returns integer rounded up to nearest whole.
• round(integer1, [NumOfDigits]): Rounds integer depending on number of digits specified, default is to nearest whole.
@Nimster
Nimster / ruby_lazy_enums.rb
Created Jul 4, 2012
Lazy enumerator helpers with Ruby
View ruby_lazy_enums.rb
# Motivation:
#
# Using lazy evaluation is the way truly functional (not just 'functional style') programming languages
# achieve performance. Ruby is not there yet for several reasons, but at least some of them can be remedied
# with basic amendments to the standard library.
#
# If you want to find, for instance, the first number n<1000 such that the approximation (1+(1/n))^n to e is
# less than a certain distance, say, eps = 0.01. If you just write
(1..1000).to_enum.map { |d| ((1+(1.0/d))**(d) - (Math::E)).abs }.find_index { |x| x < 0.01 }
# It works, and returns 134. But you calculated 1000-134=876 more values than you had to. With the code below,
@Nimster
Nimster / gist:2838511
Created May 30, 2012
Quick reference to some common R idioms/capabilities - mainly to serve as a reminder for me
View gist:2838511
### Most of the summary is taken from the awesome R twotorials at http://www.twotorials.com/ by Anthony Damico
### Some of it are my additions from my experience. This is intended so you can Ctrl+F and find what you want using
### common names of functions and concepts from other languages or statistics.
### Troubleshooting: Search http://tolstoy.newcastle.edu.au/R/ , http://www.r-bloggers.com/, http://www.rseek.org/
### Basics
traceback() # Get the call stack after an error, for debugging
32 %% 2 # == 0 mod operator
5 %/% 3 # == 1 integer division
@Nimster
Nimster / gist:2624523
Created May 6, 2012
Quick reference to some common Matlab idioms/capabilities - mainly to serve as a reminder for me
View gist:2624523
[I, J] = find(A > 0) % Col-order search
[J, I] = find(A' > 0) % Row-order (non-zero) search
arrayfun(@(x) length(x.field)>0, some_struct_array) % Apply length to 'field' in each element of a struct array
a_struct = struct('field1', value1, 'field2', value2) % Create a struct ("object")
list = [list 1] % add element to a vector
@Nimster
Nimster / ruby_summary.rb
Created Apr 8, 2012
Quick reference to some common Ruby idioms/capabilities - mainly to serve as a reminder for me
View ruby_summary.rb
## This is an example encompassing much of ruby's basic syntax.
## It is derived from various examples in the Pragmatic Bookshelf's "Programming
## Ruby" book. It is intended as a quick reference, and you should grep (search
## the file) for various terms like "splat args", "exception" or "iterator" to
## get a quick look at how they are used. It is probably very hard to make any
## sense of this by reading it linearly, nor did I attempt to have the example
## make any logical sense at all - the operations done here are not at all
## coherent
##
## This is freely distributable with the following credit:
View gist:2338235
COMMANDS
rails new demo #Create a new rails project under 'demo'
rails server #Start a local server to test the project, port 3000
rails generate controller ControllerName action1 action2
# For example 'rails generate controller say hello goodbye' gives
# say/hello and say/goodbye links, which are implemented by a class
# app/controllers/say_controller.rb with hello and goodbye methods.
@Nimster
Nimster / python_hints.py
Created Apr 8, 2012
Quick reference to some common python idioms/capabilities - mainly to serve as a reminder for me
View python_hints.py
collections.defaultdict(lambda: 0) # Hash map with default values
l = [t for t in l if not t in s] # Given a list l and a set s, returns l without all the elements in s
fun_with_many_args(*[1, 2, 3]) # Splat an array into an argument list
fun_with_named_args(**{'arg1': 1, 'arg2': 2}) # Splat a map (dictionary) for named argument functions!
@Nimster
Nimster / gist:2156727
Created Mar 22, 2012
How to compile GraphViz 2.8.2 to get the python bindings on Mac OS X Lion 10.7.3 with Xcode 4.3.1 from source
View gist:2156727
./configure --enable-python27=yes --with-png --enable-ltdl-install --with-included-ltdl --enable-perl=no CC="gcc -arch x86_64" CXX="g++ -arch x86_64"
make
sudo make install