Skip to content

Instantly share code, notes, and snippets.

View Nicktz's full-sized avatar

Nico Katzke Nicktz

View GitHub Profile
@Nicktz
Nicktz / 0_reuse_code.js
Created August 22, 2017 09:27
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
# 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)
df %>% .[!Universe %in% c("JALSH", "AS51")]
Notice the ! before the data source, not before %in%.
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),
RdsFilesIdentical <- function(RdsLocation1, RdsLocation2) {
library(fairtreeR)
load.packages()
Rds1 <- read_rds(RdsLocation1)
Rds2 <- read_rds(RdsLocation2)
identical(Rds1, Rds2)
test <- function(x) {
y <- x^2
y
}
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]
# Simple:
select_(dataframe, .dots = VectorofNames)
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.
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))