Skip to content

Instantly share code, notes, and snippets.

View andrie's full-sized avatar

Andrie de Vries andrie

View GitHub Profile
@andrie
andrie / CRAN_pkg_history.R
Last active February 26, 2020 19:38
Scrapes CRAN for historical number of packages per release
# Scrapes CRAN archives to determine the number of packages per release
# Create a list of pages to scrape, including both archive and current
extract_url <- function(){
url <- list(
archive = "https://cran-archive.r-project.org/bin/windows/contrib/",
active = "https://cran.r-project.org/bin/windows/contrib/"
)
get_urls <- function(url){
@andrie
andrie / analysis.R
Created December 27, 2015 18:34
World record runing events
library(ggplot2)
dat <- read.csv("world records.csv", stringsAsFactors = FALSE)
# Clean and transform the data --------------------------------------------
track <- within(dat, {
Time <- as.numeric(Time.in.hours)
Date <- as.Date(Date, format = "%d-%b-%y")
Speed <- Distance / Time
logDistance <- log10(Distance)
@andrie
andrie / install-and-try-wakefield.R
Created October 28, 2015 17:12
Install wakefield
# Install from github ----------------
# install.packages(c("chron", "ggplot2", "dplyr", "stringi"))
# devtools::install_github("trinker/wakefield")
# Create a sample data frame ---------
library(wakefield)
r_data_frame(
n = 500,
id,
@andrie
andrie / wakefield-example.R
Created October 28, 2015 17:09
wakefield example
library(magrittr)
library(dplyr)
library(tidyr)
library(ggplot2)
set.seed(1)
dat <- r_data_frame(12,
name,
r_series(grade, 100, relate = "+1_6")
)
library(parallel)
set.seed(1)
m <- 10000
n <- 2000
A <- matrix(runif (m*n),m,n)
setMKLthreads(1)
system.time(S <- svd (A,nu=0,nv=0))
# user system elapsed
@andrie
andrie / doSNOW.R
Created October 21, 2015 10:08
Progress bars with foreach and doSNOW
library(doSNOW)
library(tcltk)
cl <- makeSOCKcluster(2)
registerDoSNOW(cl)
pb <- txtProgressBar(max=100, style=3)
progress <- function(n) setTxtProgressBar(pb, n)
opts <- list(progress=progress)
r <- foreach(i=1:100, .options.snow=opts) %dopar% {
@andrie
andrie / 1-RODBC.R
Created October 12, 2015 11:00
Preview of using Revolution R Enterprise inside SQL Server 2016
### Connect to SQL Server using RODBC
library(RODBC)
library(magrittr)
# Connect to SQL Server using RODBC ------------------
sqlHost <- "DAA136209339.sys-sqlsvr.local"
sqlDatabase <- "RevoTestDB"
dsnString <- "driver={SQL Server};server=%s;database=%s;trusted_connection=true"
@andrie
andrie / gamma_parameters.R
Created October 7, 2015 15:19
Fit a gamma distribution based on prior information
# Objective
#
# Fit a gamma distribution knowing that:
# - 20% fall below 15 days
# - 80% fall below 60 days
# Inspired by http://www.johndcook.com/blog/2010/01/31/parameters-from-percentiles/
x <- c(0.2, 0.8)
y <- c(15, 60)
@andrie
andrie / repr options.R
Created September 25, 2015 15:16
Changing plot size in Jupyter IRkernel
library(repr)
# Change plot size to 4 x 3
options(repr.plot.width=4, repr.plot.height=3)
curve(sin(x), from = 0, to=2*pi, n = 100)
# Change plot size to 8 x 3
options(repr.plot.width=8, repr.plot.height=3)
curve(sin(x), from = 0, to=4*pi, n = 200)
@andrie
andrie / interpolate.R
Last active December 9, 2019 06:56
Interpolation and smoothing functions in R
# Generate data in the form of a sine wave
set.seed(1)
n <- 1e3
dat <- data.frame(
x = 1:n,
y = sin(seq(0, 5*pi, length.out = n)) + rnorm(n=n, mean = 0, sd=0.1)
)
approxData <- data.frame(