Skip to content

Instantly share code, notes, and snippets.

@ramhiser
ramhiser / RSQlite.r
Created June 9, 2011 23:48
Input data from a SQLite database with RSQLite
library('RSQLite')
# Establishes a connection to the specified SQLite database file.
db_filename <- "choose_filename.db3"
db_driver <- dbDriver("SQLite")
db_conn <- dbConnect(db_driver, dbname = db_filename)
# An alternative is... (not sure about the difference)
# db_conn <- dbConnect(SQLite(), dbname = db_filename)
@ramhiser
ramhiser / gist:1133190
Created August 9, 2011 01:04
Creates a zero-padded string from an integer.
# Creates a zero-padded string from an integer.
#
# The number of digits is defaulted to 3 but can be specified by the user.
# Examples:
# padded_integer(5) => "005"
# padded_integer(42) => "042"
# padded_integer(421) => "421"
# padded_integer(421, digits = 4) => "0421
#
padded_integer <- function(x, digits = 3) {
@ramhiser
ramhiser / gist:1155263
Created August 18, 2011 21:27
Error when using lubridate
# The Error
> mdy_hms(my_dates)
Error in data.frame(c("14", "19", "01"), c("14", "20"), check.names = TRUE, :
arguments imply differing number of rows: 3, 2
# The Cause
> str_split(dates, fixed(sep[1]))
[[1]]
[1] "14" "19" "01"
@ramhiser
ramhiser / gist:1202533
Created September 8, 2011 03:26
Maximum Binary Tree Sum - Project Euler #18
from numpy import array
raw_data = """75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
@ramhiser
ramhiser / gist:1378388
Created November 19, 2011 03:31
Generate Uniform Clusters and Plot with ggplot2
library('plyr')
library('reshape2')
library('ggplot2')
bivar_unif <- function(n, a1, b1, a2, b2) {
cbind(runif(n, a1, b1), runif(n, a2, b2))
}
gen_unif <- function(n = 25, delta = 0, seed = NULL) {
if(is.null(seed)) {
@ramhiser
ramhiser / gist:1378571
Created November 19, 2011 06:57
Bash Prompt for Putty using Solarized
# The colors are used with the Solarized dark color theme in Putty.
# They make look terrible if Solarized is not installed.
# Here's the link to Solarized:
# http://ethanschoonover.com/solarized
#
# I used the following website to generate my prompt:
# http://www.linuxhelp.net/guides/bashprompt/bashprompt-print.php
#
# There, I used the following code:
# <green>(<white>\d<yellow>@<white>\@<green>) (<white>\u<yellow>@<white>\h<green>) (<white>\W<green>)<space>$<space>
@ramhiser
ramhiser / gist:1378634
Created November 19, 2011 09:01
.emacs
(require 'mouse)
(xterm-mouse-mode t)
(defun track-mouse (e))
;; Save all backup file in this directory.
@ramhiser
ramhiser / gist:1378639
Created November 19, 2011 09:02
.Rprofile
.First <- function() {
options(
repos = c(CRAN = "http://cran.fhcrc.org/"),
browserNLdisabled = TRUE,
deparse.max.lines = 2
)
}
# This code is copied directly from ?savehistory
# It saves the history of commands from interactive sessions to my home path
@ramhiser
ramhiser / gist:1378640
Created November 19, 2011 09:03
.Renviron
R_LIBS=~/local/Rpackages
@ramhiser
ramhiser / gist:1392602
Created November 25, 2011 01:17
Pseudo-Random vs. Random Numbers in R
library('plyr')
library('pixmap')
library('random')
rand_bit_matrix <- function(num_rows = 500, num_cols = 500,
max_n_random.org = 10000, seed = NULL) {
# I have copied the following function directly from help("integer").
is.wholenumber <- function(x, tol = .Machine$double.eps^0.5) {
abs(x - round(x)) < tol
}