This file contains 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
// Use Gists to store code you would like to remember later on | |
console.log(window); // log the "window" object to the console |
This file contains 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
# Shows step by step how to plot drawdowns in R using ggplot2. | |
if(!require(tbl2xts)) install.packages("tbl2xts") | |
if(!require(tidyverse)) install.packages("tidyverse") | |
library(tbl2xts) | |
library(tidyverse) | |
library(PerformanceAnalytics) | |
# Now use TRI data: | |
data(TRI) |
This file contains 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
df %>% .[!Universe %in% c("JALSH", "AS51")] | |
Notice the ! before the data source, not before %in%. |
This file contains 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
dplyr allows the user to avoid loops. | |
The loops are replaced by group_by() if data is well gathered into a tidy data frame. | |
E.g.: | |
data <- data.frame( | |
date = rep(c(1,2,3,4), each=25), | |
Tickers = rep(c("A", "B", "C", "D")), | |
Returns = rnorm(100), |
This file contains 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
RdsFilesIdentical <- function(RdsLocation1, RdsLocation2) { | |
library(fairtreeR) | |
load.packages() | |
Rds1 <- read_rds(RdsLocation1) | |
Rds2 <- read_rds(RdsLocation2) | |
identical(Rds1, Rds2) |
This file contains 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
test <- function(x) { | |
y <- x^2 | |
y | |
} |
This file contains 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
As I answered here: http://stackoverflow.com/a/37939267/4198868 | |
To remove all columns with only zeros: | |
dfzeroremoved <- df %>% .[,colSums(. != 0) > 0] | |
To remove all columns with only NA: | |
dfzeroremoved <- df %>% .[,colSums(!is.na(.)) > 0] |
This file contains 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
# Simple: | |
select_(dataframe, .dots = VectorofNames) |
This file contains 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(far) | |
data <- | |
data.frame(x = rnorm(30, 0, 1.5), | |
y = rnorm(30, 0, 1.5), | |
z = rnorm(30, 0, 1.5)) | |
y <- | |
orthonormalization(data,basis=FALSE, norm=TRUE) | |
# basis = TRUE squares columns. |
This file contains 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
data %>% mutate_each(funs(scale), X, Y) | |
data %>% mutate_each_(funs(scale),vars=c("X","Y")) | |
Which will scale the selected columns to be N(0,1). | |
E.g.: | |
data <- data.frame(x = rnorm(10, 30, .2), | |
y = runif(10, 3, 5), | |
z = runif(10, 10, 20)) |