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
# This is the code to generate .RData file. | |
# Note - this will create the environment which, if loaded, works as is on my PC. | |
# See environment on right of your screen - it saves those elements. | |
# Keep this environment ONLY relevant to what you need help with (don't clutter - filter your df) | |
# E.g. | |
rm(list=ls()) | |
library(tidyverse) |
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
# This function takes in a dataframe, a column name (to be arranged), and the order with which to arrange it. | |
# Use example: | |
library(tidyverse) | |
set.seed(123) | |
df <- tibble::tibble( Charcol = LETTERS[1:10], Numcol = rnorm(10)) | |
order <- df %>% arrange(desc(Numcol)) %>% pull(Charcol) %>% unique | |
plot_orderset <- function(df, Column, Order) { | |
df[,Column][[1]] <- factor(df[,Column][[1]], levels = Order) | |
df |
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
#' @title theme_psec1 | |
#' @description Plotting theme for ggplot: with boundary | |
#' @param FontType Check available fonts using: windowsFonts() | |
#' @return plot theme | |
#' @examples | |
#' g + theme_psec1(title.size = ggpts(35), axis.size.title = ggpts(35), grid_Col = "#FFFFFF") # Remove grids | |
#' g + theme_psec1(title.size = ggpts(35), axis.size.title = ggpts(35)) | |
#' | |
#' # Custom Caption used as: | |
#' # labs(caption= "First~line \n italic('and a second,')~bold('fancy one') \n 'also,'~integral(f(x)*dx, a, b)~'for good measure'") |
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
# Source this gist to load functions in local environment that allow dendogram plotting using ggplot2 in R. | |
# Written by Nico Katzke (nfkatzke@gmail.com) | |
#' @title cluster_aux | |
#' @description Auxilliary functions for doing nice dendograms in R. See here for more info: https://atrebas.github.io/post/2019-06-08-lightweight-dendrograms/ | |
#' @return Loads several auxilliary functions used for dendogram plotting | |
#' @examples | |
#' \dontrun{ | |
#' cluster_aux() | |
#' mtc <- scale(mtcars) |
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 setup for both SQL Server and MySQL | |
## SQL Server | |
Connecting to SQL server from linux (Useful answer: https://stackoverflow.com/a/51266453/4198868): | |
First, get Server's IP. In linux / Terminal in Rstudio use | |
ping PSC-CPT-SQL2 |
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
# Copy the below into your R console and run the code to see illustration of safe return calculation. | |
# Motivation: | |
# Depending on your data structure and assumptions - you should choose how to square your return and weight dates. | |
# I mostly want to merge on the day returns to on the day weights. | |
# E.g. | |
# date Stock Returns weights | |
# 2018-04-01 XXX 0.025 0.05 |
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
# Example created with datapasta | |
library(tidyverse) | |
library(lubridate) | |
library(tbl2xts) | |
df <- | |
bind_cols( | |
date = as.Date(c("2018-01-01", "2018-01-02", "2018-01-03", "2018-01-04", "2018-01-05", "2018-01-06", "2018-01-07", "2018-01-08", "2018-01-09", "2018-01-10", "2018-01-11", "2018-01-12", "2018-01-13", "2018-01-14", "2018-01-15", "2018-01-16", "2018-01-17", "2018-01-18", "2018-01-19", "2018-01-20")), | |
tibble::tribble( | |
~A_Shares, ~X_Shares, ~C_Shares, ~A_Price, ~X_Price, ~C_Price, |
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
# Function built by Nico Katzke (nfkatzke@gmail.com) | |
# geom_dual : Dual Axis ggplot function - specifically for time-series (or dataframe with a column "date" or "Date" | |
# Provide the function with two dataframes that have overlapping date columns, where the values to be plotted are given by Value. | |
# E.g.: | |
library(tidyverse) | |
library(lubridate) | |
if(!require(rmsfuns)) install.packages("rmsfuns") | |
df1 <- tibble(date = rmsfuns::dateconverter(ymd(20170101), ymd(20180101), Transform = "weekdays")[1: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
From: http://feedproxy.google.com/~r/RBloggers/~3/xIaQcw7UIFs/ | |
library(tidyverse) | |
bare to quosure: quo | |
bare_to_quo <- function(x, var){ | |
x %>% select(!!var) %>% head(1) | |
} | |
bare_to_quo(mtcars, quo(cyl)) | |
## cyl |
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
http://www.ggplot2-exts.org/gallery/ | |
Excellent miscellaneous plotting calls |
NewerOlder