Skip to content

Instantly share code, notes, and snippets.

@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]))
}
@ZeccaLehn
ZeccaLehn / winLinSet.R
Created March 31, 2016 18:09
[R] Desktop Directory Function for Windows or Linux
# Allows for working directory to be used on Windows or Linux
projectFolder <- "YOURDESKTOPFOLDER"
if(Sys.info()['sysname'] == "Linux"){
setwd( paste0( Sys.getenv("HOME") , paste0("/Desktop/", projectFolder, "/") ) )
} else {
X <- Sys.getenv('HOME')
setwd(gsub("Documents", paste0("/Desktop/", projectFolder, "/"), X))
rm(X)
}
@ZeccaLehn
ZeccaLehn / quandlRfundamentals.R
Created April 14, 2016 18:37
Free Fundamentals Time Series from Quandl (Tangible Book and Equity) [R]
# SF0/MSFT_EQUITY_MRY # Equity
# SF0/MSFT_PB_MRY # Price / Book Value
# SF0/MSFT_SHARESWADIL_MRY # Wavg shares diluted
# SF0/MSFT_SHARESWADIL_MRY # Tangible per share
library(Quandl)
Quandl.api_key("YOURQUANDLKEY")
Ticker <- "FB"
@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 / arulesBinary.r
Last active May 26, 2016 18:27
Adapted Arules Association Rules from Binary Table in [R]
# Adapted Arules Association Rules from Binary Table in R
# https://monsiterdex.wordpress.com/2013/11/01/market-basket-analysis-apriori-algorithm-in-r-part-01/
# https://github.com/A01203249/YouTube-Videos/blob/master/R_code_%26_data/titanic.R
# https://www.youtube.com/watch?v=DQGJhZNhG4M
# http://stackoverflow.com/questions/11659128/how-to-use-cast-or-another-function-to-create-a-binary-table-in-r
dat <- read.table(textConnection("
Group Response
A A
A E
@ZeccaLehn
ZeccaLehn / absPath.py
Last active June 1, 2016 17:23
Change Absolute Path Directory in Python 3
from os.path import expanduser
home = expanduser("~")
path = os.path.join(home, 'Desktop')
os.chdir(path)
os.listdir()