This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##### 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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))}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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% |