Skip to content

Instantly share code, notes, and snippets.

@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
@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 / GenderWageGap.R
Last active April 10, 2018 17:23
Code to produce comparison of wage gap based on OECD data.
library(OECD)
library(dplyr)
library(ggplot2)
## Get the latest OECD data on decile ratios of gross earnings.
## This is the dataset that contains information on wage gaps.
oecdData <- get_dataset("DEC_I")
## Prepare the plot data
gwg <-
@JoFAM
JoFAM / InstallRoxygen601.R
Last active September 11, 2018 18:33
Install roxygen 6.0.1 for Windows
# Currently roxygen 6.1.0 doesn't read the collate field correctly, so once you use @include anywhere
# in your package, you run into problems. The error you get, is a 'Permission denied' error. If that
# occurs, best thing to do for now is to install the previous version of roxygen until
# the problem is solved.
#
# I've made a binary build for Windows available. You can download it manually from following link:
# https://jorismeys.stackstorage.com/s/co8cKzyRemVHZef
# Alas the filesharing I use doesn't allow automatic downloading through download.file(), so you
# have to go to that link and click on Download to get the actual zip file.
#
@JoFAM
JoFAM / lift_dlvsrbind.txt
Last active December 19, 2018 12:00
lift_dl() versus rbind()
``` r
library(magrittr)
library(purrr)
#>
#> Attaching package: 'purrr'
#> The following object is masked from 'package:magrittr':
#>
#> set_names
library(microbenchmark)
@JoFAM
JoFAM / SeaLevelSimulation.R
Created February 12, 2019 11:01
Small script to create a simulation of linear sealevel rise under different speeds.
library(tidyr)
library(ggplot2)
# I look at 4 different rates here. The rate for the period 2000-2018 was approximately 3 mm / year.
rates <- c(0.15,0.2,0.3, 0.4)
# This creates a tibble (dataset) with the sea level rise until 2100 depending on the rate
mydata <- tibble(
years = seq(2018,2100, by=1),
slow = seq_along(years)*rates[1],
@JoFAM
JoFAM / HADCRUTMEDIAN.R
Created March 11, 2019 16:54
Quick plot of the hadcrut data.
# Download median of regional timeseries
temp <- readLines("https://www.metoffice.gov.uk/hadobs/hadcrut4/data/current/time_series/HadCRUT.4.6.0.0.monthly_ns_avg.txt")
temp <- data.frame(do.call(rbind, strsplit(temp," ")),
stringsAsFactors = FALSE)
temp[-1] <- lapply(temp[-1], as.numeric)
temp$year <- as.numeric(gsub("/.*","",temp[,1]))
temp$month <- as.numeric(gsub(".*/","",temp[,1]))
temp$time <- temp$year + temp$month/12.5
@JoFAM
JoFAM / PottmpOcean.R
Created March 31, 2019 18:04
Calculate departure from average ocean temperature for 1980-2019
## ----setup, include=FALSE------------------------------------------------
knitr::opts_chunk$set(echo = TRUE)
# Needed so the code runs later on.
datadir <- "pottmp_data"
fnames <- paste("pottmp",1980:2019,"nc", sep = ".")
## ----Download information, eval=FALSE------------------------------------
## # ONLY RUN THIS ONCE TO DOWNLOAD THE DATA
## # This downloads 39 files of appx 144 Mb each!
## # Create a directory to store the data.
@JoFAM
JoFAM / server.R
Created September 12, 2019 16:19
An example of a shiny App that can be run from a Gist
library(shiny)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
mysamples <- reactive({
fdist <- switch(input$dist,
Normal = rnorm,
Exponential = rexp,
Poisson = function(n) rpois(n,4))
@JoFAM
JoFAM / UAHtemps.R
Created November 19, 2019 13:31
Plotting monthly global temperatures from UAH dataset
# Plot UAH monthly temperatures
# Joris Meys
# Date last modified: 2019-11-18
# Create a temporary filename
fname <- tempfile()
# Download data into the temporary file
download.file("https://www.nsstc.uah.edu/data/msu/v6.0/tlt/uahncdc_lt_6.0.txt",destfile = fname)