Skip to content

Instantly share code, notes, and snippets.

@ZeccaLehn
ZeccaLehn / MAImp.test.r
Last active August 29, 2015 14:19 — forked from timelyportfolio/MAImp.test.r
Portfolio Plots
# Loads current gist with function
# https://gist.github.com/timelyportfolio/1405187
# dev.off(); plot(1)
library(devtools)
source_gist("1405187")
require(quantmod)
require(PerformanceAnalytics)
@ZeccaLehn
ZeccaLehn / rawToTable.R
Last active April 20, 2016 01:41
[R] Raw to Table
dat <- read.table(textConnection("
date rtn
01/03/2012 0.25%
01/04/2012 0.01%
01/05/2012 0.02%
01/06/2012 -0.06%
01/09/2012 0.11%
01/10/2012 -0.01%
01/11/2012 0.00%
@ZeccaLehn
ZeccaLehn / sinkPrint.R
Last active August 29, 2015 14:22
Sink and Print from a text file in R
sink("file.txt", append = T,
split = FALSE) # Direct text to "file.txt"
print("hello world")
print("hello world again")
shell.exec("file.txt") # Open text file in editor
unlink("file.txt") # Closes sink and now prints all to console
closeAllConnections()
@ZeccaLehn
ZeccaLehn / CheckLoadPackages.R
Last active November 11, 2016 22:05
[R] Check and Load Packages
# Allows for install check and automatic download
# Note: Rerun line 6 after CTRL + ALT + F10 Restart
# Adapted from: http://stackoverflow.com/questions/4090169/elegant-way-to-check-for-missing-packages-and-install-them
packages <- c("data.table", "dplyr", "doParallel")
packagesCheck <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(packagesCheck)>0) {install.packages(packagesCheck)}; rm(packagesCheck)
lapply(packages, function(x) {do.call("require", list(x))})
@ZeccaLehn
ZeccaLehn / MergeOverlappingTables.R
Created June 26, 2015 03:26
[R] Merge Overlapping Tables using Multiple Keys (Data.Table)
library(data.table)
DM = data.table(X = 1:5 , B = c("a", letters[6:7], NA, NA), D = c("g",
letters[22:25]), Y = c("j", letters[6:9]) )
keycols = names(DM)
setkeyv(DM, keycols)
DT = data.table(X = 1:5, B = c("a", letters[6:9]),
D = c("g", letters[2:5]), Y = c("j", letters[16:19]))
@ZeccaLehn
ZeccaLehn / runLib
Last active August 29, 2015 14:24
[R] Load and Reload Libraries after Restart with runLib().
packages <- c("data.table", "dplyr", "doParallel") # List of libraries
runLib <- function(packages = packages) {
packagesCheck <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(packagesCheck)) {install.packages(packagesCheck)}; rm(packagesCheck)
lapply(packages, function(x) {do.call("require", list(x))})
}
runLib(packages) # Rerun this line after RESTART in R and add packages as needed
@ZeccaLehn
ZeccaLehn / dendogramHeatmaps.R
Last active August 29, 2015 14:25
[R] Dendogram heatmaps in R -- both static and D3.js comparisons
##### Dendogram Reatmaps in R -- both static and D3.js comparisons #####
library(d3heatmap)
# Comperables (w/scaled values -- better than below)
heatmap(scale(mtcars), Colv=T)
d3heatmap(scale(mtcars), Colv=T, colors="YlOrRd")
# Comperables (w/actual values -- not as strong as above)
heatmap(as.matrix(mtcars), scale="column", Colv=T)
@ZeccaLehn
ZeccaLehn / CurrencyDownloader.R
Created August 3, 2015 01:58
[R] Quandl RevolutionR Modified Example (Currencies)
# Modified from RevolutionR
# http://blog.revolutionanalytics.com/2013/06/a-mini-tutorial-for-quandl.html
##### API Key #####
token <- 'Your Private API' # Sign up with Quandl to get a token
#-----------------------------------------------------
library(Quandl) # Quandl package
library(ggplot2) # Package for plotting
library(reshape2) # Package for reshaping data
@ZeccaLehn
ZeccaLehn / ReverseFrequency.R
Created August 23, 2015 23:18
[R] Unpacking a frequency Table and Creating Frequency Table -- Titanic
library(datasets)
library(DescTools)
(freqTab <- data.frame(Titanic))
# Class Sex Age Survived Freq
# 1 1st Male Child No 0
# 2 2nd Male Child No 0
# 3 3rd Male Child No 35
# 4 Crew Male Child No 0
@ZeccaLehn
ZeccaLehn / classVector
Created August 31, 2015 17:21
[R] Class of Each Feature via Iteration -- from a Data.Table Object
library(data.table)
trainRaw <- fread("train.csv", header = TRUE, sep = ",")
classVec <- NULL
for(i in 1:length(names(trainRaw))){
classVec[i] <- class(unlist(trainRaw[, i, with=F]))
}