Skip to content

Instantly share code, notes, and snippets.

View daattali's full-sized avatar

Dean Attali daattali

View GitHub Profile
@daattali
daattali / spinme
Created February 1, 2015 03:16
Add this at top of R script so that "source"ing the script will automatically render a markdown+HTML
#+ spinme-chunk, eval = FALSE, echo = FALSE
.scriptName <- basename(sys.frame(1)$ofile)
.spinOutput <- knitr::spin(.scriptName, knit = TRUE)
stop(sprintf("Ignore this error - your output is ready at:\n%s",
normalizePath(.spinOutput)),
call. = FALSE)
#+ spinme-done, eval = TRUE, echo = TRUE
@daattali
daattali / S3_parent_params.R
Created May 8, 2015 03:12
S3 method does not show parent params in autocomplete
# There are two humans: `human` and `angry_human`. They only differ in the fact
# that angry humans have a frown on their face.
# Example code:
# dean <- human("Dean")
# ex <- angry_human("Ex")
# plot(dean)
# plot(ex)
# When plotting `dean`, RStudio shows me all the available parameters.
@daattali
daattali / beepr_error.R
Created May 11, 2015 01:58
beepr_error
# rasmusab/beepr package is great for telling you when your R code finishes running.
# But if the code throws an error, the beep will never come, and you won't know that
# it finished running until you visually check.
# Solution: change the error handler to a failure beep.
# Example
foo <- function(success = TRUE) {
if (!success) {
stop("Error!")
}
# Suppose a function has an argument "x". If a "child function" has multiple
# argument beginning with "x" and I want to pass the param "x" to its parent,
# I get error: `argument n matches multiple formal arguments`
# The reason this happens is clear, but at 3am my brain is firing blanks
# trying to find a solution. Help!
library(magrittr)
foo <- function(x, ...) UseMethod("foo")
foo.parent <- function(x, n = 5, ...) n
@daattali
daattali / bcl-data.csv
Created November 26, 2015 06:55
BC Liquor Store data
Type Subtype Country Name Alcohol_Content Price Sweetness
WINE TABLE WINE RED CANADA COPPER MOON - MALBEC 14 30.99 0
WINE TABLE WINE WHITE CANADA DOMAINE D'OR - DRY 11.5 32.99 0
WINE TABLE WINE RED CANADA SOMMET ROUGE 12 29.99 0
WINE TABLE WINE WHITE CANADA MISSION RIDGE - PREMIUM DRY WHITE 11 33.99 1
WINE TABLE WINE RED UNITED STATES OF AMERICA ZINFANDEL - BIG HOUSE CARDINAL ZIN 13.5 36.99 0
WINE TABLE WINE RED FRANCE LE VILLAGEOIS RED - CELLIERS LA SALLE 11 34.99 0
WINE TABLE WINE RED CANADA SAWMILL CREEK - MERLOT 12.5 119 0
WINE TABLE WINE WHITE CANADA SOLA 12 32.99 0
WINE TABLE WINE WHITE CANADA GANTON & LARSEN PROSPECT - PINOT BLANC BIRCH CANOE 2011 11.5 13.99 0
n <- 50
a <- 2
b <- -3
sigSq <- 0.5
x <- runif(n)
norm <- rnorm(n, sd = sqrt(sigSq))
y <- a + b * x + norm
plot(x,y)
abline(a, b, col = "blue")
n <- 50
a <- 2
b <- -3
sigSq <- 0.5
x <- runif(n)
norm <- rnorm(n, sd = sqrt(sigSq))
y <- a + b * x + norm
plot(x,y)
abline(a, b, col = "blue")
Dean Attali's View of the World
========================================================
First, let's load the data and the required libraries (in this case, just lattice for plotting)
**Note:** The data is available [here](http://www.stat.ubc.ca/~jenny/notOcto/STAT545A/examples/gapminder/data/gapminderDataFiveYear.txt)
```{r}
library("lattice")
gDat <- read.delim("gapminderDataFiveYear.txt")
```
Dean Attali
=================================
**STAT 545A hw 3**
**Sept 22 2013**
Exercises done:
* Average GDP/cap in each continent when the data was first and last collected **(easy)**
* Trimmed mean statistics for life expectancy in each continent for every year **(fun)**
* Absolute and relative world population in each of the continents **(very fun)**
* A list of all countries that at some point had their population size decrease **(very fun)**
Dean Attali
=================================
**STAT 545A hw 3**
**Sept 22 2013**
Exercises done:
* Average GDP/cap in each continent when the data was first and last collected **(easy)**
* Trimmed mean statistics for life expectancy in each continent for every year **(fun)**
* Absolute and relative world population in each of the continents **(very fun)**
* A list of all countries that at some point had their population size decrease **(very fun)**