Skip to content

Instantly share code, notes, and snippets.

@JoFAM
JoFAM / BenchmarkForLoopOuter.R
Created November 30, 2017 14:34
For loops versus outer: You might be surprised by which is faster.
# x is a dataset, and one wants to construct a new dataset created by x minus the 5 "quartiles",
# being 0%, 25%, 50%, 75% and 100% quantiles.
# This constructs the data
x <- rnorm(1e6)
quart <- quantile(x)
# The original code of one of my students. I loathed two things:
# - the use of a for loop when one could use outer()
# - the growing of an object
oorspronkelijk <- function(x, quart){
@JoFAM
JoFAM / HurricaneDamage.R
Created November 30, 2017 11:00
Calculating the fraction of real GDP lost in hurricane damage for the most costly hurricane years.
# Calculation of hurricane damage as percentage of
# real GDP in the US.
# The data:
# damage taken from:
# https://www.wunderground.com/cat6/2017-us-hurricane-damages-206-billion-highest-record
# real GDP taken from
# https://fred.stlouisfed.org/series/GDPCA
year <- c(2017,1893,2005,1928,1960,2012,1969,1947,1954,1945)
# damage in billion of dollars, adjusted to 2017 dollars