Skip to content

Instantly share code, notes, and snippets.

@rpietro
rpietro / qplot_hadley.r
Created August 13, 2013 14:32
script from Hadley Wickham's book with some comments and additions
#install.packages(ggplot2)
require(ggplot2)
set.seed(1410) # Make the sample reproducible
dsmall <- diamonds[sample(nrow(diamonds), 100), ]
head(dsmall)
?diamonds
qplot(carat, price, data = diamonds)
qplot(log(carat), log(price), data = diamonds)
@rpietro
rpietro / data_management.r
Created August 14, 2013 12:05
script on data management from a wide variety of sources including dalgaard's book and others
# reading data into R: http://goo.gl/bAUmj
#install.packages("ISwR")
library("ISwR")
#factors
pain <- c(0,3,2,2,1)
fpain <- factor(pain,levels=0:3)
levels(fpain) <- c("none","mild","medium","severe")
require.install <- function(pkg, github=""){
req <- suppressWarnings(
suppressMessages(
require(package=paste(pkg), character.only=TRUE)
)
);
#print(github);
if(!req){
installOk <- FALSE;
if(length(github)<=0){
@rpietro
rpietro / knitpost.r
Created August 15, 2013 18:13
knitpost function from Jason Fisher's blog at http://jfisher-usgs.github.io/r/2012/07/03/knitr-jekyll/ - i just brought the function to lower case so that i can remember how to call it
KnitPost <- function(input, base.url = "/") {
require(knitr)
opts_knit$set(base.url = base.url)
fig.path <- paste0("figs/", sub(".Rmd$", "", basename(input)), "/")
opts_chunk$set(fig.path = fig.path)
opts_chunk$set(fig.cap = "center")
render_jekyll()
knit(input, envir = parent.frame())
}
@rpietro
rpietro / hadley_vocabulary.rmd
Created August 16, 2013 04:18
Hadley's vocabulary with additional definitions and reproducible examples
# Hadley's vocabulary with additional definitions and reproducible examples
This document takes the original [Vocabulary chapter](https://github.com/hadley/devtools/wiki/Vocabulary) from Hadley Wickham's in progress book "[Advanced R development: making reusable code](https://github.com/hadley/devtools/wiki)" and adds definitions and reproducible examples using [knitr](http://yihui.name/knitr/). Unless otherwise noted, the examples come straight from the help for each function or are modified versions from those examples.
```{r}
library("knitr")
```
@rpietro
rpietro / regression_dalgaard.r
Created August 16, 2013 12:23
regression scripts from Dalgaard's book
# regression models from dalgaard
library("ISwR")
?thuesen
thuesen
attach(thuesen)
lm(short.velocity~blood.glucose)
summary(lm(short.velocity~blood.glucose)) #the median should not be far from zero, and the minimum and maximum should be roughly equal in absolute value
plot(blood.glucose,short.velocity)
abline(lm(short.velocity~blood.glucose))
@rpietro
rpietro / hadley_functions.r
Created August 17, 2013 00:06
R scripts from Hadley Wickham's chapter on functions
# This is a collection of scripts from Hadley Wickham's chapter on functions at https://github.com/hadley/devtools/wiki/Functions
# Components of a function
f <- function(x) x
f
formals(f) # argument list
body(f) # code inside the function
environment(f) # how variables are found
@rpietro
rpietro / matrix_regression.r
Created August 19, 2013 01:59
explaining regression through matrix multiplication
set.seed(123)
dependent <- rnorm(100)
set.seed(1234)
independent <- dependent*(rnorm(100))
identity <- rep(1, 100)
identity
independent2 <- cbind (identity, independent2)
independent2
@rpietro
rpietro / sql_statement_rmysql.txt
Created August 29, 2013 13:23
sql statements and use within rmysql
brew install mysql
show databases;
exit;
mysql -u root -p
UPDATE mysql.user SET Password = PASSWORD('foo.bar') WHERE User = 'root';
FLUSH PRIVILEGES;
@rpietro
rpietro / mandelbrot.R
Created September 1, 2013 00:35
mandelbrot fractal from http://goo.gl/oINBbd
library(ggplot2)
max_iter=25
cl=colours()
step=seq(-2,0.8,by=0.005)
points=array(0,dim=c(length(step)^2,3))
t=0
for(a in step)
{