Skip to content

Instantly share code, notes, and snippets.

@1beb
1beb / fundserv_codes.R
Created May 25, 2011 23:03
Pull FundServ Data into R
library(XML)
theurl <- "http://www.fundserv.com/services/code-lists.php?status_type=1&file_type=d"
dealer.codes <- readHTMLTable(theurl)
n.rows <- unlist(lapply(dealer.codes, function(t) dim(t)[1]))
dealer.codes <- dealer.codes[[which.max(n.rows)]]
@1beb
1beb / rep_v_for.r
Created May 26, 2011 20:15
System.time comparison of rep vs. for loop
system.time(for(x in 1:10000) {print("Hello world")}) ## 0.54
system.time(print(rep("Hello world",10000))) ## 0.12
@1beb
1beb / double_y_base.r
Created May 26, 2011 20:41
Double Y Axis Plots with Base
x <- rnorm(100)*20
y <- rnorm(100)*40
plot(1:100, x, type="l", col="blue", xlab="X axis label", ylab="Left legend")
par(new=TRUE)
plot(1:100, y, type="l", ann=FALSE, yaxt="n")
axis(4)
legend(x="topleft", bty="n", lty=c(1,1), col=c("blue","black"), legend=c("Left", "Right"))
@1beb
1beb / double_y_plot.r
Created May 27, 2011 15:01
Doubly Y Axis Plots with Base (Daily Currency Data)
x <- structure(list(date = structure(c(14613, 14614, 14615, 14616,
14617, 14620, 14621, 14622, 14623, 14624, 14627, 14628, 14629,
14630, 14631, 14634, 14635, 14636, 14637, 14638, 14641, 14642,
14643, 14644, 14645, 14648, 14649, 14650, 14651, 14652, 14655,
14656, 14657, 14658, 14659, 14662, 14663, 14664, 14665, 14666,
14669, 14670, 14671, 14672, 14673, 14676, 14677, 14678, 14679,
14680, 14683, 14684, 14685, 14686, 14687, 14690, 14691, 14692,
14693, 14694, 14697, 14698, 14699, 14700, 14701, 14704, 14705,
14706, 14707, 14708, 14711, 14712, 14713, 14714, 14715, 14718,
14719, 14720, 14721, 14722, 14725, 14726, 14727, 14728, 14729,
@1beb
1beb / VIX_v_GSPC.r
Created May 28, 2011 20:32
Milktrader Double Y VIX v. GSPC
require("quantmod")
getSymbols("^GSPC;^VIX")
VIX <- as.data.frame(unlist(VIX))
GSPC <- as.data.frame(unlist(GSPC))
y1 <- data.frame(Date=as.Date(row.names(VIX),"%Y-%m-%d"),"VIX"=VIX$VIX.Close)
row.names(y1) <- NULL
y2 <- data.frame(Date=as.Date(row.names(GSPC),"%Y-%m-%d"),"GSPC"=GSPC$GSPC.Close)
row.names(y2) <- NULL
plot(y1$Date, y1$VIX, type="l", col="blue", xlab="Time", ylab="Closing Price")
@1beb
1beb / part1-ggplot2intro.r
Created June 14, 2011 20:27
Part 1: ggplot2 Intro
## Title: ggplot2 Introduction
## Description: This line by line analysis, provides an introduction to ggplot2
## Created by: Brandon Bertelsen: Research Manager, Credo Consulting Inc.
# install.packages("ggplot2")
## Load Library
library(ggplot2)
# load sample data
@1beb
1beb / cleaner.txt
Created June 16, 2011 20:24
Remove [...] and (...)
Regex: \[.*\] finds anything in [...]
Regex: \(.*\) finds anything in (...)
@1beb
1beb / part2.r
Created June 19, 2011 20:50
PartII ggplot2 Introduction
## Title: ggplot2 Introduction: Part 2
## Description: This line by line analysis, provides an introduction to ggplot2. How to deal with factors, subsetting and labels
## Created by: Brandon Bertelsen: Research Manager, Credo Consulting Inc.
# install.packages("ggplot2")
## Load Library
library(ggplot2)
# load sample data
@1beb
1beb / part3.r
Created June 20, 2011 01:48
Part III: ggplot2 introduction, faceting examples
## Title: ggplot2 Introduction: Part 3
## Description: This line by line analysis, provides an introduction to ggplot2. About faceting.
## Created by: Brandon Bertelsen: Research Manager, Credo Consulting Inc.
# Still working with the same dataset, let's revisit some of our earlier plots.
ggplot(d, aes(price)) + geom_histogram()
# Apply a basic facet to view more of your data
ggplot(d, aes(price)) + geom_histogram() + facet_grid(~cut)
@1beb
1beb / part4.r
Created June 20, 2011 04:53
Part IV: ggplot2 Introduction
## Title: ggplot2 Introduction: Part IV
## Description: This line by line analysis, provides an introduction to ggplot2. Time series.
## Created by: Brandon Bertelsen: Research Manager, Credo Consulting Inc.
# Review the economics data set from the ggplot2 intro
e <- economics
str(e)
# Let's look at geom_line
ggplot(e, aes(date, uempmed)) + geom_line()